mysql分组后取每组前几条记录

查找 from_id = test2 的记录,按target_id 分组,create_time 降序,取每组前2条记录

SELECT from_id,target_id,create_time,FROM_UNIXTIME(create_time/1000,"%Y-%m-%d %T") createTime from biz_jiguang_im_msg AS t1
where
(SELECT COUNT(1) FROM biz_jiguang_im_msg AS t2 WHERE t2.from_id=t1.from_id AND t2.target_id=t1.target_id AND t2.create_time>t1.create_time) < 2
AND t1.from_id >= ‘test2’
ORDER BY t1.target_id,t1.create_time DESC;
mysql分组后取每组前几条记录_第1张图片

你可能感兴趣的:(mysql分组后取每组前几条记录)