首页 > 数据库技术 > 详细

mysql每组前N条

时间:2018-01-09 17:50:56      阅读:190      评论:0      收藏:0      [点我收藏+]
SELECT * from (
select
  case when t.province=@prov then @rownum:=@rownum+1
  else @rownum:=1 end rownum,
  case when t.province=@prov then 0
  else @prov:=t.province end prov,

  t.*
from
  (

    SELECT 110000 province,1231 money
    UNION all
    SELECT 110000 province,123121 money
    UNION all
    SELECT 110000 province,1231231 money
    UNION all
    SELECT 120000 province,121231 money
    UNION all
    SELECT 120000 province,123121 money
    UNION all
    SELECT 130000 province,12131 money
    UNION all
    SELECT 130000 province,12231 money
    UNION all
    SELECT 140000 province,123311 money

  ) t,(select @rownum:=0) rn,(select @prov:=‘‘) prov
ORDER BY t.province,t.money desc
) tt
where tt.rownum<=2
;

 

mysql每组前N条

原文:https://www.cnblogs.com/warling/p/8252667.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!