2021-05-17
Posted Java.xu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-05-17相关的知识,希望对你有一定的参考价值。
2021最新mysql版SQL查询练习,入门级版本
如果你能坚持自己做完,那么恭喜你,你的SQL查询算入门了!!!千万不要直接看答案!!!一定要自己思考!!!不会再去看答案参考,SQL不一定要和我的一样,希望加入自己的思考。
--建表
--学生表
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
--课程表
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
--教师表
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
--成绩表
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
--插入学生表测试数据
INSERT INTO Student VALUES('01' , '赵雷' , '1990-01-01' , '男');
INSERT INTO Student VALUES('02' , '钱电' , '1990-12-21' , '男');
INSERT INTO Student VALUES('03' , '孙风' , '1990-05-20' , '男');
INSERT INTO Student VALUES('04' , '李云' , '1990-08-06' , '男');
INSERT INTO Student VALUES('05' , '周梅' , '1991-12-01' , '女');
INSERT INTO Student VALUES('06' , '吴兰' , '1992-03-01' , '女');
INSERT INTO Student VALUES('07' , '郑竹' , '1989-07-01' , '女');
INSERT INTO Student VALUES('08' , '王菊' , '1990-01-20' , '女');
--课程表测试数据
INSERT INTO Course VALUES('01' , '语文' , '02');
INSERT INTO Course VALUES('02' , '数学' , '01');
INSERT INTO Course VALUES('03' , '英语' , '03');
--教师表测试数据
INSERT INTO Teacher VALUES('01' , '张三');
INSERT INTO Teacher VALUES('02' , '李四');
INSERT INTO Teacher VALUES('03' , '王五');
--成绩表测试数据
INSERT INTO Score VALUES('01' , '01' , 80);
INSERT INTO Score VALUES('01' , '02' , 90);
INSERT INTO Score VALUES('01' , '03' , 99);
INSERT INTO Score VALUES('02' , '01' , 70);
INSERT INTO Score VALUES('02' , '02' , 60);
INSERT INTO Score VALUES('02' , '03' , 80);
INSERT INTO Score VALUES('03' , '01' , 80);
INSERT INTO Score VALUES('03' , '02' , 80);
INSERT INTO Score VALUES('03' , '03' , 80);
INSERT INTO Score VALUES('04' , '01' , 50);
INSERT INTO Score VALUES('04' , '02' , 30);
INSERT INTO Score VALUES('04' , '03' , 20);
INSERT INTO Score VALUES('05' , '01' , 76);
INSERT INTO Score VALUES('05' , '02' , 87);
INSERT INTO Score VALUES('06' , '01' , 31);
INSERT INTO Score VALUES('06' , '03' , 34);
INSERT INTO Score VALUES('07' , '02' , 89);
INSERT INTO Score VALUES('07' , '03' , 98);
-- 该设置是关闭mysql对group by的安全检查,select查询的字段应该都在group by中,这也是mysql的规范,但实际应用中我们可能不是很想要这个规范
SET GLOBAL sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
SET SESSION sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT s.*,sc.`s_score` '语文',sc2.`s_score` '数学' FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id` AND sc.`c_id`='01'
LEFT JOIN `score` sc2 ON sc2.`s_id`=s.`s_id` AND sc2.`c_id`='02'
WHERE sc.`s_score`>sc2.`s_score`
-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT s.*,sc.`s_score` '语文',sc2.`s_score` '数学' FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id` AND sc.`c_id`='01'
LEFT JOIN `score` sc2 ON sc2.`s_id`=s.`s_id` AND sc2.`c_id`='02'
WHERE sc.`s_score`<sc2.`s_score`
-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT s.`s_id`,s.`s_name`,ROUND(AVG(sc.`s_score`),2) FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id`
GROUP BY s.`s_id`
HAVING AVG(sc.`s_score`)>=60
-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
-- (包括有成绩的和无成绩的)
SELECT s.`s_id`,s.`s_name`,(CASE WHEN ROUND(AVG(sc.`s_score`),2) IS NULL THEN 0 ELSE ROUND(AVG(sc.`s_score`),2) END) '平均分' FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id`
GROUP BY s.`s_id` HAVING AVG(sc.`s_score`)<60 OR AVG(sc.`s_score`) IS NULL
-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT s.`s_id`,s.`s_name`,COUNT(DISTINCT sc.`c_id`) '课程数',(CASE WHEN ROUND(SUM(sc.`s_score`),2) IS NULL THEN 0 ELSE ROUND(SUM(sc.`s_score`),2) END) '总成绩' FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id`
GROUP BY s.`s_id`
-- 6、查询"李"姓老师的数量
SELECT COUNT(t.t_id) FROM `teacher` t WHERE t.`t_name` LIKE '李%'
-- 7、查询学过"张三"老师授课的同学的信息
SELECT DISTINCT s.* FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id`
LEFT JOIN `course` c ON c.`c_id`=sc.`c_id`
LEFT JOIN `teacher` t ON t.`t_id`=c.`t_id`
WHERE t.`t_name` ='张三'
-- 8、查询没学过"张三"老师授课的同学的信息
-- 张三老师教的课
SELECT c.* FROM `course` c
LEFT JOIN `teacher` t ON t.`t_id`=c.`t_id`
WHERE t.`t_name`='张三'
-- 有张三老师课成绩的st.s_id
SELECT s.`s_id` FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id`
LEFT JOIN `course` c ON c.`c_id`=sc.`c_id`
LEFT JOIN `teacher` t ON t.`t_id`=c.`t_id`
WHERE t.`t_name`='张三'
GROUP BY s.`s_id`
-- 改进
SELECT sc.`s_id` FROM `score` sc
WHERE sc.`c_id` IN (
SELECT c.`c_id` FROM `course` c LEFT JOIN `teacher` t ON t.`t_id`=c.`t_id` WHERE t.`t_name`='张三'
)
-- 不在上面查到的st.s_id的学生信息,即没学过张三老师授课的同学信息
SELECT s.* FROM `student` s
WHERE s.`s_id` NOT IN(
SELECT sc.`s_id` FROM `score` sc
WHERE sc.`c_id` IN (
SELECT c.`c_id` FROM `course` c LEFT JOIN `teacher` t ON t.`t_id`=c.`t_id` WHERE t.`t_name`='张三'
))
GROUP BY s.`s_id`
-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT s.* FROM `student` s
INNER JOIN `score` sc ON sc.`s_id`=s.`s_id` AND sc.`c_id`='01'
INNER JOIN `score` sc2 ON sc2.`s_id`=s.`s_id` AND sc2.`c_id`='02'
GROUP BY s.`s_id`
HAVING COUNT(sc.`c_id`)>0 AND COUNT(sc2.`c_id`)>0
-- 网友提供的思路(厉害呦~):
SELECT st.*
FROM student st
INNER JOIN score sc ON sc.`s_id`=st.`s_id`
GROUP BY st.`s_id`
HAVING SUM(IF(sc.`c_id`="01" OR sc.`c_id`="02" ,1,0))>1
-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT s.* FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id` AND sc.`c_id`='01'
LEFT JOIN `score` sc2 ON sc2.`s_id`=s.`s_id` AND sc2.`c_id`='02'
GROUP BY s.`s_id`
HAVING COUNT(sc.`c_id`)>0 AND COUNT(sc2.`c_id`)=0
-- 11、查询没有学全所有课程的同学的信息
SELECT s.* FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id` AND sc.`c_id`='01'
LEFT JOIN `score` sc2 ON sc2.`s_id`=s.`s_id` AND sc2.`c_id`='02'
LEFT JOIN `score` sc3 ON sc3.`s_id`=s.`s_id` AND sc3.`c_id`='03'
GROUP BY s.`s_id`
HAVING COUNT(sc.`c_id`)=0 OR COUNT(sc2.`c_id`)=0 OR COUNT(sc3.`c_id`)=0
-- 太复杂,下次换一种思路,看有没有简单点方法
-- 此处思路为查学全所有课程的学生id,再内联取反面
SELECT * FROM student WHERE s_id NOT IN (
SELECT st.s_id FROM student st
INNER JOIN score sc ON sc.s_id = st.s_id AND sc.c_id="01"
WHERE st.s_id IN (
SELECT st2.s_id FROM student st2
INNER JOIN score sc2 ON sc2.s_id = st2.s_id AND sc2.c_id="02"
) AND st.s_id IN (
SELECT st2.s_id FROM student st2
INNER JOIN score sc2 ON sc2.s_id = st2.s_id AND sc2.c_id="03"
))
-- 来自一楼网友的思路,左连接,根据学生id分组过滤掉 数量小于 课程表中总课程数量的结果(show me his code),简洁不少。
SELECT st.* FROM Student st
LEFT JOIN Score S
ON st.s_id = S.s_id
GROUP BY st.s_id
HAVING COUNT(c_id)<(SELECT COUNT(c_id) FROM Course)
-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT DISTINCT s.* FROM `student` s
LEFT JOIN `score` sc2 ON sc2.`s_id`=s.`s_id`
WHERE sc2.`c_id` IN(SELECT sc.`c_id` FROM `score` sc WHERE sc.`s_id`='01')
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT s.* FROM `student` s
LEFT JOIN `score` sc2 ON sc2.`s_id`=s.`s_id`
GROUP BY s.`s_id`
HAVING GROUP_CONCAT(DISTINCT sc2.`c_id`)=(SELECT GROUP_CONCAT(DISTINCT sc.`c_id`) FROM `score` sc WHERE sc.`s_id`='01')
-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT s.* FROM `student` s
WHERE s.`s_id` NOT IN(
SELECT s2.`s_id` FROM `score` s2
INNER JOIN `course` c ON c.`c_id`=s2.`c_id`
INNER JOIN `teacher` t ON t.`t_id`=c.`t_id` AND t.`t_name`='张三')
-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT s.`s_id`,s.`s_name`,ROUND(AVG(sc.`s_score`),2) FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id`
WHERE s.`s_id` IN (
SELECT sc2.`s_id` FROM `score` sc2
WHERE sc2.`s_score` < 60 OR sc2.`s_score` IS NULL
GROUP BY sc2.`s_id`
HAVING COUNT(sc2.`s_id`)>=2)
GROUP BY s.`s_id`
-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT s.`s_id`,s.`s_name`,s.`s_birth`,s.`s_sex`,sc.`s_score` FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id` AND sc.`c_id`='01'
GROUP BY s.`s_id`
HAVING sc.`s_score`<60
ORDER BY sc.`s_score` DESC
-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
-- 可加round,case when then else end 使显示更完美
SELECT s.`s_name`,IF(sc.`s_score` IS NULL,0,sc.`s_score`) '语文',IF(sc2.`s_score` IS NULL,0,sc2.`s_score`) '数学',
IF(sc3.`s_score` IS NULL,0,sc3.`s_score`) '英语',IF(ROUND(AVG(sc4.`s_score`),2) IS NULL ,0,ROUND(AVG(sc4.`s_score`),2)) '平均分' FROM `student` s
LEFT JOIN `score` sc ON sc.`s_id`=s.`s_id` AND sc.`c_id`='01'
LEFT JOIN `score` sc2 ON sc2.`s_id`=s.`s_id` AND sc2.`c_id`='02'
LEFT JOIN `score` sc3 ON sc3.`s_id`=s.`s_id` AND sc3.`c_id`='03'
LEFT JOIN `score` sc4 ON sc4.`s_id`=s.`s_id`
GROUP BY s.`s_id`
ORDER BY AVG(sc4.`s_score`) DESC
-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT c.c_id,c.c_name,MAX(sc.s_score) "最高分",MIN(sc2.s_score) "最低分",AVG(sc3.s_score) "平均分"
,((SELECT COUNT(s_id) FROM score WHERE s_score>=60 AND c_id=c.c_id )/(SELECT COUNT(s_id) FROM score WHERE c_id=c.c_id)) "及格率"
,((SELECT COUNT(s_id) FROM score WHERE s_score>=70 AND s_score<80 AND c_id=c.c_id )/(SELECT COUNT(s_id) FROM score WHERE c_id=c.c_id)) "中等率"
,((SELECT COUNT(s_id) FROM score WHERE s_score>=80 AND s_score<90 AND c_id=c.c_id )/(SELECT COUNT(s_id) FROM score WHERE c_id=c.c_id)) "优良率"
,((SELECT COUNT(s_id) FROM score WHERE s_score>=90 AND c_id=c.c_id )/(SELECT COUNT(s_id) FROM score WHERE c_id=c.c_id)) "优秀率"
FROM course c
LEFT JOIN score sc ON sc.c_id=c.c_id
LEFT JOIN score sc2 ON sc2.c_id=c.c_id
LEFT JOIN score sc3 ON sc3.c_id=c.c_id
GROUP BY c.c_id
-- 19、按各科成绩进行排序,并显示排名(实现不完全)
-- mysql没有rank函数
-- 加@score是为了防止用union all 后打乱了顺序
-- 20、查询学生的总成绩并进行排名
SELECT s.*,IF(SUM(sc.`s_score`) IS NULL ,0,SUM(sc.`s_score`)) '总分' FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
GROUP BY s.`s_id`
ORDER BY SUM(sc.`s_score`)
-- 21、查询不同老师所教不同课程平均分从高到低显示
SELECT t.`t_id`,t.`t_name`,c.`c_id`,c.`c_name`,AVG(sc.`s_score`) FROM `teacher` t
LEFT JOIN `course` c ON t.`t_id`=c.`t_id`
LEFT JOIN `score` sc ON c.`c_id`=sc.`c_id`
GROUP BY t.`t_id`,c.`c_id`
ORDER BY AVG(sc.`s_score`)
-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]
SELECT c.`c_id`,c.`c_name`
,(SELECT COUNT(`s_score`) FROM `score` WHERE `c_id`=c.`c_id` AND `s_score`>=0 AND `s_score`<60) '0~60'
,(SELECT COUNT(`s_score`) FROM `score` WHERE `c_id`=c.`c_id` AND `s_score`>=60 AND `s_score`<70) '60~70'
,(SELECT COUNT(`s_score`) FROM `score` WHERE `c_id`=c.`c_id` AND `s_score`>=70 AND `s_score`<85) '70~85'
,(SELECT COUNT(`s_score`) FROM `score` WHERE `c_id`=c.`c_id` AND `s_score`>=85 AND `s_score`<=100) '85~100'
FROM `score` sc
LEFT JOIN `course` c ON c.`c_id`=sc.`c_id`
GROUP BY c.`c_id`
-- 24、查询学生平均成绩及其名次
SET @i=0;
SELECT a.*,@i:=@i+1 FROM (
SELECT s.`s_id`,s.`s_name`,ROUND(IF(AVG(sc.`s_score`) IS NULL,0,AVG(sc.`s_score`)),2) '平均分' FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
GROUP BY s.`s_id` ORDER BY AVG(sc.`s_score`)
) a
-- 25、查询各科成绩前三名的记录
(SELECT s.`s_id`,s.`s_name`,c.`c_id`,c.`c_name`,sc.`s_score` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
INNER JOIN `course` c ON c.`c_id`=sc.`c_id` AND c.`c_id`='01'
GROUP BY s.`s_id`,c.`c_id` ORDER BY sc.`s_score` DESC LIMIT 3)
UNION ALL(
SELECT s.`s_id`,s.`s_name`,c.`c_id`,c.`c_name`,sc.`s_score` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
INNER JOIN `course` c ON c.`c_id`=sc.`c_id` AND c.`c_id`='02'
GROUP BY s.`s_id`,c.`c_id` ORDER BY sc.`s_score` DESC LIMIT 3)
UNION ALL(
SELECT s.`s_id`,s.`s_name`,c.`c_id`,c.`c_name`,sc.`s_score` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
INNER JOIN `course` c ON c.`c_id`=sc.`c_id` AND c.`c_id`='03'
GROUP BY s.`s_id`,c.`c_id` ORDER BY sc.`s_score` DESC LIMIT 3)
-- 26、查询每门课程被选修的学生数
SELECT sc.`c_id`,COUNT(DISTINCT sc.`s_id`) FROM `score` sc
GROUP BY sc.`c_id`
SELECT c.c_id,c.c_name,COUNT(1) FROM course c
LEFT JOIN score sc ON sc.c_id=c.c_id
INNER JOIN student st ON st.s_id=c.c_id
GROUP BY st.s_id
-- 27、查询出只有两门课程的全部学生的学号和姓名
SELECT s.`s_id`,s.`s_name` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
GROUP BY s.`s_id`
HAVING COUNT(DISTINCT sc.`c_id`)=2
-- 28、查询男生、女生人数
SELECT s.`s_sex`,COUNT(*) FROM `student` s
GROUP BY s.`s_sex`
-- 29、查询名字中含有"风"字的学生信息
SELECT * FROM `student` s WHERE s.`s_name` LIKE '%风%'
-- 30、查询同名同性学生名单,并统计同名人数
SELECT s.*,COUNT(1) FROM `student` s
GROUP BY s.`s_name`,s.`s_sex`
HAVING COUNT(1)>1
-- 31、查询1990年出生的学生名单
SELECT * FROM `student` s WHERE s.`s_birth` LIKE '1990%'
-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT sc.`c_id`,c.`c_name`,ROUND(AVG(sc.`s_score`),2) '平均分' FROM `score` sc
INNER JOIN `course` c ON c.`c_id`=sc.`c_id`
GROUP BY sc.`c_id`
ORDER BY AVG(sc.`s_score`) DESC,sc.`c_id` ASC
-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
SELECT s.`s_id`,s.`s_name`,AVG(sc.`s_score`) FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
INNER JOIN `course` c ON c.`c_id`=sc.`c_id`
GROUP BY s.`s_id`
HAVING AVG(sc.`s_score`)>=85
-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
SELECT s.`s_name`,sc.`s_score` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id` AND sc.`c_id`=(SELECT c_id FROM`course` WHERE c_name='数学')
GROUP BY s.`s_id`
HAVING sc.`s_score`<60
-- 35、查询所有学生的课程及分数情况;
SELECT s.`s_id`,s.`s_name`,c.`c_name`,sc.`s_score` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
INNER JOIN `course` c ON c.`c_id`=sc.`c_id`
GROUP BY sc.`s_id`,sc.`c_id`
-- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
SELECT s.`s_name`,c.`c_name`,sc.`s_score` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
INNER JOIN `course` c ON c.`c_id`=sc.`c_id`
WHERE s.`s_id` IN (
SELECT s2.`s_id` FROM `student` s2
LEFT JOIN `score` sc2 ON s2.`s_id`=sc2.`s_id`
GROUP BY s2.`s_id`
HAVING MIN(sc2.`s_score`) > 70
)
-- 37、查询不及格的课程
SELECT c.`c_name`,s.`s_name`,sc.`s_score` FROM `score` sc
RIGHT JOIN `course` c ON c.`c_id`=sc.`c_id`
INNER JOIN `student` s ON s.`s_id`=sc.`s_id`
WHERE sc.`s_score`<60
-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
SELECT s.`s_name`,s.`s_id`,sc.`s_score` FROM `student` s
INNER JOIN `score` sc ON s.`s_id`=sc.`s_id` AND sc.`c_id` AND sc.`s_score`>80
-- 39、求每门课程的学生人数
SELECT c.`c_name`,COUNT(DISTINCT sc.`s_id`) '课程人数' FROM `score` sc
LEFT JOIN `course` c ON c.`c_id`=sc.`c_id`
GROUP BY c.`c_id`
-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
SELECT s.*,MAX(sc.`s_score`),sc.`c_id` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
WHERE sc.`c_id` =(SELECT t_id FROM `teacher` t WHERE t.`t_name`='张三')
-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT s.`s_id`,s.`s_name`,c.`c_id`,c.`c_name`,sc.`s_score` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
LEFT JOIN `course` c ON c.`c_id`=sc.`c_id`
WHERE (
SELECT COUNT(1) FROM `student` s2
LEFT JOIN `score` sc2 ON s2.`s_id`=sc2.`s_id`
LEFT JOIN `course` c2 ON c2.`c_id`=sc2.`c_id`
WHERE sc2.`s_score`=sc.`s_score` AND c2.`c_id`!=c.`c_id`
)>=1
-- 42、查询每门功成绩最好的前两名
(SELECT *,sc.`s_score` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id` AND sc.`c_id`='01'
ORDER BY sc.`s_score` DESC LIMIT 2)
UNION ALL(
SELECT *,sc.`s_score` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id` AND sc.`c_id`='02'
ORDER BY sc.`s_score` DESC LIMIT 2)
UNION ALL(
SELECT *,sc.`s_score` FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id` AND sc.`c_id`='03'
ORDER BY sc.`s_score` DESC LIMIT 2)
-- 借鉴(更准确,漂亮):
SELECT s.`s_name`,a.s_id,a.c_id,a.s_score FROM score a
INNER JOIN `student` s ON s.`s_id`=a.`s_id`
WHERE (SELECT COUNT(1) FROM score b WHERE b.c_id=a.c_id AND b.s_score>=a.s_score)<=2 ORDER BY a.c_id
-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,
-- 若人数相同,按课程号升序排列
SELECT c.`c_id`,c.`c_name`,COUNT(sc.`s_id`) FROM `course` c
LEFT JOIN `score` sc ON c.`c_id`=sc.`c_id`
GROUP BY c.`c_id`
HAVING COUNT(sc.`s_id`)>5
ORDER BY COUNT(sc.`s_id`) DESC,c.`c_id` ASC
-- 44、检索至少选修两门课程的学生学号
SELECT s.* FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
GROUP BY s.`s_id`
HAVING COUNT(sc.`c_id`)>=2
-- 45、查询选修了全部课程的学生信息
SELECT s.* FROM `student` s
LEFT JOIN `score` sc ON s.`s_id`=sc.`s_id`
GROUP BY s.`s_id`
HAVING COUNT(sc.`c_id`)=3
-- 46、查询各学生的年龄
SELECT s.`s_name`,TIMESTAMPDIFF(YEAR,s.`s_birth`,NOW()) FROM `student` s
-- 47、查询本周过生日的学生
-- 此处可能有问题,week函数取的为当前年的第几周,2017-12-12是第50周而2018-12-12是第49周,可以取月份,day,星期几(%w),
-- 再判断本周是否会持续到下一个月进行判断,太麻烦,不会写
-- week(data,model)data为日期,model指定星期几开始计算为一周
SELECT * FROM `student` s
WHERE WEEK(NOW(),1)=WEEK(s.`s_birth`,1)
-- 48、查询下周过生日的学生
SELECT * FROM `student` s
WHERE WEEK(NOW(),1)+1=WEEK(s.`s_birth`,1)
-- 49、查询本月过生日的学生
SELECT * FROM `student` s
WHERE MONTH(NOW())=MONTH(s.`s_birth`)
-- 50、查询下月过生日的学生
-- 注意:当 当前月为12时,用month(now())+1为13而不是1,可用timestampadd()函数或mod取模
SELECT *,MONTH(s.`s_birth`),MONTH(TIMESTAMPADD(MONTH,1,NOW())) FROM `student` s
WHERE MONTH(TIMESTAMPADD(MONTH,1,NOW()))=MONTH(s.`s_birth`)
或
SELECT st.* FROM student st WHERE (MONTH(NOW()) + 1) MOD 12 = MONTH(DATE_FORMAT(st.s_birth,'%Y%m%d'))
以上是关于2021-05-17的主要内容,如果未能解决你的问题,请参考以下文章
《安富莱嵌入式周报》第213期:2021.05.17--2021.05.23
《安富莱嵌入式周报》第212期:2021.05.11--2021.05.17