ID TOTAL
1 57000
1 52000
1 400
1 451000
1 400
1 1050
1 40000
2 5300
2 25000
2 80000
2 42000
2 5000
2 9000
2 4000
2 2000
2 28000
2 371000
3 300000
3 55327
4 9000
4 3000
4 2510
... ....
表结构很简单,需求是这样的
ID<3的取对应TOTAL倒序后的5条记录
ID<5的取对应TOTAL倒序后的7条记录
....
能否用一句sql搞定?
1 57000
1 52000
1 400
1 451000
1 400
1 1050
1 40000
2 5300
2 25000
2 80000
2 42000
2 5000
2 9000
2 4000
2 2000
2 28000
2 371000
3 300000
3 55327
4 9000
4 3000
4 2510
... ....
表结构很简单,需求是这样的
ID<3的取对应TOTAL倒序后的5条记录
ID<5的取对应TOTAL倒序后的7条记录
....
能否用一句sql搞定?
select * from tb where id<3 order by total desc limit 5
select * from tb where id<5 order by total desc limit 7
union all
select * from tb where id<5 order by total desc limit 7
union all
(select * from tb where id<5 order by total desc limit 6,1)