码迷,mamicode.com
首页 > 数据库 > 详细

mysql每组前N条

时间:2018-01-09 18:45:37      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:gpo   money   div   mon   row   sele   union   font   esc   

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条

标签:gpo   money   div   mon   row   sele   union   font   esc   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!