mysql take out the top two scores of each subject

 

 

There is only one table, self-association, compare scores, take the top two

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;

 

If there are two second places with the same score, both will be displayed

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324727836&siteId=291194637