Php + mysql compare another field value with the same field value MySQL PHP
Table structure
Id int primary key auto-increment
Mydate datetime
Price int
The value of the flag int is 1, 2, 3, 4, 5, and 6, and the price with this value is given priority.
Example
Id mydate price flag
1 2013-05-30 200 2
2 2013-05-30 300 3
3 2013-05-31 150 2
4 2013-05-31 200 3
5 2013-06-01 200 2
6 2013-06-01 300 3
Result:
Id mydate price flag
1 2013-05-30 300 3
2 2013-05-31 200 3
3 2013-06-01 350 3
How to write php code? I haven't written php code for a long time. thank you!
Reply to discussion (solution)
Select * from (select * from tt order by flag desc) t group by mydate;
Select id, mydate, max (price) as price, flag from (select * from tbl_name order by price desc) t group by mydate
Isn't this grouping query?
SELECT * FROM (SELECT * FROM tORDER BY flag DESC , price DESC)ttGROUP BY mydate
Does the landlord compare the flag size first, and compare the price size if the flag size is the same? Try the method on the 4th floor (in fact, I don't know this problem, but I fully think about the 2nd floor; I originally wanted to group by xx order by xxx, if the group by clause is used, the first clause is taken directly, and the order by clause is invalid. That's why I wrote it on the second floor)
Select * from tt a where not exists (select 1 from tt
Where a. mydate = mydate and a. flag Or
A. mydate = mydate and a. flag = flag and a. price )
Http://bbs.csdn.net/topics/390476456 this post I took the SQL section to ask. I confirmed that my method seems to be okay, and I have gained some benefits today.
The great gods upstairs are very enthusiastic. They answered in my post and also answered here. But we rarely use it like this. Standard SQL syntax
To WWWWA: how can I add this condition during the specified date period?
To WWWWA: how can I add this condition during the specified date period?
Select * from tt a where not exists (select 1 from tt
Where (a. mydate = mydate and a. flag Or
A. mydate = mydate and a. flag = flag and a. price And mydate between '2017-05-30 'and '2017-06-01'
)
And a. mydate between '2017-05-30 'and '2017-06-01'