In the project there is a need to query a table, group by, and then the group's count (*) in reverse order, take out each group inside the latest record of the publication time, the former colleague SQL wrote this
SELECT * from ( SELECT * from Table WHEREID= ? andStatus= ? ORDER byCreated_timeDESC ) asaGROUP byA.GroupORDER by Count(*)DESCLIMITTen
However, the final data is out of order, that is, the data in the group is not the latest in the publication time, very strange. Finally, I added a limit to this statement, as follows
SELECT * from ( SELECT * from Table WHEREID= ? andStatus= ? ORDER byCreated_timeDESCLIMIT10000 ) asaGROUP byA.GroupORDER by Count(*)DESCLIMITTen
Although the limit is 10000, but the entire table is actually more than 3,000 data, but so that the data found in the group is the latest data. I was thinking that the limit in MySQL is not like Java's volatile, with the code optimization to keep the variable relative position "magic"
The magic of limit within MySQL