Mysql 分组后在分组内排序、每个分组中取前N条

2021-06-17  本文已影响0人  青玉_f18c
SELECT type,name,row_num from (
  SELECT type,name,IF(@bak=type,@rownum:=@rownum+1,@rownum:=1) as row_num, @bak:=type
   FROM
   ( SELECT type,name from products order by type,name desc ) a , ( SELECT @rownum:=0,@bak:='') b 
) c WHERE c.row_num <=2

说明:

上一篇下一篇

猜你喜欢

热点阅读