SQL | MySQL group query for the latest piece of data in each group

It is suitable for the case of primary key self-increment, even if the creation time is in a few cases, it does not affect, because the search is based on the self-incremented primary key (the latest data primary key is definitely the largest, that is, the latest data is inserted into the database)
Insert picture description here

select id, msg_content, msg_create_time
from safety_promotion_msg spm
         inner join (select max(id) max_id from safety_promotion_msg group by msg_content) a on a.max_id = spm.id;

Insert picture description here
If the primary key does not increase automatically, take the maximum time, that is max(msg_create_time), use the timestamp in the creation time field to prevent the creation time from being consistent and ensure the highest accuracy.

Guess you like

Origin blog.csdn.net/y1534414425/article/details/108537785