MySQL中查询获取每个班级成绩前三名的学生信息

 CREATE TABLE t_testscore( pk_id INT PRIMARY KEY, c_name VARCHAR(50) , c_score INT, c_class INT )DEFAULT CHARSET=utf8; INSERT INTO t_testscore VALUES (1, '张三6', 66, 1),(2, '张三5', 65, 1),(3, '张三4', 64, 1), (4, '张三3', 63, 1),(5, '张三2', 62, 1); INSERT INTO t_testscore VALUES (11, '李四6', 76, 2),(12, '李四5', 75, 2),(13, '李四4', 74, 2), (14, '李四3', 73, 2),(15, '李四2', 72, 2); SELECT * FROM t_testscore t WHERE EXISTS(SELECT COUNT(*) FROM t_testscore ts WHERE ts.c_score>=t.c_score GROUP BY ts.c_class HAVING COUNT(*)<=3) ORDER BY c_class,c_score DESC;

转载于:https://www.cnblogs.com/nyist-xsk/p/10636985.html

    原文作者:weixin_30577801
    原文地址: https://blog.csdn.net/weixin_30577801/article/details/95460547
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞