MySQL gets the latest records based on the update time based on a field group

Source: Internet
Author: User

I now have a table of statistics that you want to group on a field and then, based on the update time, get the latest record for each group. The name feels quite simple a need, however no idea, of course, is asked Niang.

Degree Niang's answer is very unified, however all do not work, are error, do not know is because of mysql5.7 reason, but I remember group by also can not find the whole record, why Baidu can, think no solution!

Find an example from the Internet:

Each gets the latest record of last_updated_date according to the USER_ID group.

Select Id,user_id,problems,last_updated_date from (select * from t_iov_help_feedback order by U ser_id, last_updated_date DESC) b GROUP by b.user_id;

Obviously, this will be an error in my SQL.

So I used the correlation query on followed by two conditions

SELECT * FROM (select User_id,max (last_updated_date) group by user_id) as temp left join T_iov_help_feedback as T on temp. user_id=t.user_id and Temp.last_updated_date=t.last_updated_date;

So we can find out. Write so much first, and then have time to study the argument carefully.

MySQL gets the latest records based on the update time based on a field group

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.