mysql 实现分组取出各自组内排序前三的数据
利用变量实现
set @c_id:=null, @rownum:=0;
select
*
from
(SELECT t.*,
@rownum:= (case when @c_id = t.c_id then @rownum + 1 else 1 end) count,
@c_id:=t.c_id
FROM t_class_message t
order by t.c_id asc, t.create_time desc ) T
where count < 4 ;
浙公网安备 33010602011771号