首页  编辑  

最快的求每个分组最后一个记录的方法

Tags: /MySQL/   Date Created:

最快的求每个分组最后一个记录的方法  

The fastest way to get the last record/last time record of each group in SQL:

For example:

SELECT B.*

FROM (

       SELECT id_field, MAX(time_field) AS "time_field"

       FROM a_table

       GROUP BY id_field

) A

LEFT JOIN a_table B

ON A.time_field = B.time_field AND A.id_field = B.id_field