mysql每组前N条
时间:2021-07-01 10:21:17
帮助过:15人阅读
* 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