Sql取出各科分数前三名的学生,Sql各科成绩前三的学生
Posted 蕃薯耀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sql取出各科分数前三名的学生,Sql各科成绩前三的学生相关的知识,希望对你有一定的参考价值。
Sql取出各科分数前三名的学生,Sql各科成绩前三的学生
================================
©Copyright 蕃薯耀 2021-06-09
https://www.cnblogs.com/fanshuyao/
一、取出各科成绩前三的学生
#取出各科成绩前三的学生 CREATE TABLE stu_score( id INT PRIMARY KEY AUTO_INCREMENT, stu_id INT, stu_name VARCHAR(50), clazz VARCHAR(20), score DOUBLE, remark VARCHAR(50) ); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (1, \'小赵\', \'语文\', 80); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (1, \'小赵\', \'数学\', 85); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (1, \'小赵\', \'英语\', 90); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (1, \'小赵\', \'化学\', 90); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (2, \'小钱\', \'语文\', 88); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (2, \'小钱\', \'数学\', 88); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (2, \'小钱\', \'英语\', 98); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (2, \'小钱\', \'化学\', 92); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (3, \'小孙\', \'语文\', 70); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (3, \'小孙\', \'数学\', 78); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (3, \'小孙\', \'英语\', 96); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (3, \'小孙\', \'化学\', 95); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (4, \'小李\', \'语文\', 85); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (4, \'小李\', \'数学\', 98); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (4, \'小李\', \'英语\', 88); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (4, \'小李\', \'化学\', 98); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (5, \'小王\', \'语文\', 90); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (5, \'小王\', \'数学\', 80); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (5, \'小王\', \'英语\', 85); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (5, \'小王\', \'化学\', 78); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (6, \'小陈\', \'语文\', 87); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (6, \'小陈\', \'数学\', 97); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (6, \'小陈\', \'英语\', 84); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (6, \'小陈\', \'化学\', 80); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (7, \'小朱\', \'语文\', 80); INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (7, \'小朱\', \'数学\', 88);#和小钱88分一样,并列第三 INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (7, \'小朱\', \'英语\', 98);#并列第一 INSERT INTO stu_score(stu_id,stu_name,clazz,score) VALUES (7, \'小朱\', \'化学\', 95);#并列批二 SELECT * FROM stu_score ss ORDER BY ss.`clazz`,ss.`score` DESC; #取出各科成绩前三的学生 #1、取出一名学生A某个学科的成绩 #2、然后和所有的学生同一个学科的成绩比较 #3、当存在该学科的成绩比学生A的成绩大且总人数不大于3,或者该学科成绩没有大于学生A的,则表示学生A的成绩在前三 #4、然后根据学科、分数(降序)排序取出 SELECT * FROM stu_score ss1
WHERE (
SELECT COUNT(*) FROM stu_score ss2
WHERE ss2.`clazz`=ss1.`clazz` AND ss2.`score` > ss1.`score`
) < 3
ORDER BY ss1.`clazz`,ss1.`score` DESC;
最后的查询结果:
id stu_id stu_name clazz score remark ------ ------ -------- ------ ------ -------- 16 4 小李 化学 98 (NULL) 12 3 小孙 化学 95 (NULL) 28 7 小朱 化学 95 (NULL) 14 4 小李 数学 98 (NULL) 22 6 小陈 数学 97 (NULL) 6 2 小钱 数学 88 (NULL) 26 7 小朱 数学 88 (NULL) 7 2 小钱 英语 98 (NULL) 27 7 小朱 英语 98 (NULL) 11 3 小孙 英语 96 (NULL) 17 5 小王 语文 90 (NULL) 5 2 小钱 语文 88 (NULL) 21 6 小陈 语文 87 (NULL)
截图:
二、取出各科成绩都不小于85分的学生
#取出各科成绩都不小于85分的学生
#如果一个学生所有学科的分数中,最小的都>=85,则表示该学生每科成绩都不小于85
SELECT stu_name FROM stu_score GROUP BY stu_name HAVING MIN(score) >=85;
查询结果:
stu_name ---------- 小李 小钱
(时间宝贵,分享不易,捐赠回馈,^_^)
================================
©Copyright 蕃薯耀 2021-06-09
https://www.cnblogs.com/fanshuyao/
以上是关于Sql取出各科分数前三名的学生,Sql各科成绩前三的学生的主要内容,如果未能解决你的问题,请参考以下文章