分区取前几名,后几名

select * from sc where (select count(*) from sc sc2 where sc2.score>sc.score and sc2.class=sc.class)<=2 order by sc.class,sc.score desc;
select * from sc having (select count(*) from sc t1 where t1.class=sc.class and t1.score>=sc.score)<=3;
select * from sc where exists (select count(*) from sc sc2 where sc2.score>sc.score and sc2.class=sc.class having count(*)<=2) order by sc.class,sc.score desc;
select t1.sid,t1.class,t1.score from sc t1,sc t2 where t1.class=t2.class and t2.score>=t1.score group by t1.sid,t1.class,t1.score having count(t2.sid)<=3 order by t1.class,t1.score desc;

原文地址:https://www.cnblogs.com/zuoxingyu/p/2948354.html