mysql分组获取前n条数据
-
下面sql解释: 根据create_by分组,每一个分组内取create_time最新的3条数据
SELECT *
from (
SELECT t1.*,
(SELECT count(*) + 1
FROM query_record t2
WHERE t2.create_by = t1.create_by
AND t2.create_time > t1.create_time) top
FROM query_record t1
) t
where top <= 3
order by t.create_by, top;