mysql 取出每科成绩前两名

 

 

只有一张表,自关联 ,比较分数,取前两名

SELECT * FROM stuscore A
WHERE
(SELECT COUNT(DISTINCT score) FROM stuscore B WHERE A.subject = B.subject AND B.score>=A.score)<=2
ORDER BY A.score DESC;

 

如果成绩相同的第二名有两个会都展示出来

 

posted @ 2018-04-23 17:32  lyon♪♫  阅读(396)  评论(0编辑  收藏  举报