Php + mysql compare the value of another field when the field values are the same

Source: Internet
Author: User
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'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.