非原创,根据
-- https://www.cnblogs.com/coder-wf/p/11128033.html
-- 建表
-- 学生表
-- sim nvarchar 可以解决中文乱码问题
DROP DATABASE simzhang;
CREATE DATABASE IF NOT EXISTS simzhang;
USE simzhang;
-- SHOW VARIABLES LIKE 'character_set_database';
-- ALTER DATABASE simzhang CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` NVARCHAR(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` NVARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
-- 教师表
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` NVARCHAR(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);
-- 准备条件,去掉 sql_mode 的 ONLY_FULL_GROUP_BY 否则此种情况下会报错:
-- Expression #1 of select list is not in group by clause and contains nonaggregated column 'userinfo.
-- 原因:
-- MySQL 5.7.5和up实现了对功能依赖的检测。如果启用了only_full_group_by SQL模式(在默认情况下是这样),
-- 那么MySQL就会拒绝选择列表、条件或顺序列表引用的查询,这些查询将引用组中未命名的非聚合列,而不是在功能上依赖于它们。
-- (在5.7.5之前,MySQL没有检测到功能依赖项,only_full_group_by在默认情况下是不启用的。关于前5.7.5行为的描述,请参阅MySQL 5.6参考手册。)
-- 执行以下个命令,可以查看 sql_mode 的内容。
SHOW SESSION VARIABLES;
SHOW GLOBAL VARIABLES;
SELECT @@sql_mode;
-- 更改
SET GLOBAL sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
SET SESSION sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
-- sim 2020年2月1日15:21:11
select st.* , sc1.s_score as '语文', sc2.s_score as '数学' from student st
left join score sc1 on st.s_id = sc1.s_id and sc1.c_id = '01'
left join score sc2 on st.s_id = sc2.s_id and sc2.c_id = '02'
where sc1.s_score > sc2.s_score;
SELECT st.*,sc.s_score AS '语文' ,sc2.s_score '数学'
FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id='01'
LEFT JOIN score sc2 ON sc2.s_id=st.s_id AND sc2.c_id='02'
WHERE sc.s_score>sc2.s_score
-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
-- sim 2020年2月1日15:25:30
select st.*, sc1.s_score as '语文', sc2.s_score as '数学' from student st
left join score sc1 on st.s_id = sc1.s_id and sc1.c_id = '01'
left join score sc2 on st.s_id = sc2.s_id and sc2.c_id = '02'
where sc1.s_score < sc2.s_score;
SELECT st.*,sc.s_score '语文',sc2.s_score '数学' FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id='01'
LEFT JOIN score sc2 ON sc2.s_id=st.s_id AND sc2.c_id='02'
WHERE sc.s_score<sc2.s_score;
-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
-- sim 2020年2月1日15:31:23
select st.s_id, st.s_name, round(avg(sc.s_score),2) as 平均分 from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id
having avg(sc.s_score) >= 60;
SELECT st.s_id,st.s_name,ROUND(AVG(sc.s_score),2) "平均成绩" FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY st.s_id HAVING AVG(sc.s_score)>=60;
-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
-- (包括有成绩的和无成绩的)
-- sim 2020年2月1日15:50:28
select st.s_id , st.s_name, round(ifnull(avg(sc.s_score), 0),2) as 平均成绩 from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id
having avg(sc.s_score) < 60 or avg(sc.s_score) is null;
SELECT st.s_id,st.s_name,(CASE WHEN ROUND(AVG(sc.s_score),2) IS NULL THEN 0 ELSE ROUND(AVG(sc.s_score),2) END ) "平均成绩" FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY st.s_id HAVING AVG(sc.s_score)<60 OR AVG(sc.s_score) IS NULL;
-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
-- sim 2020年2月1日15:58:07
select st.*, round(ifnull(count(sc.c_id),0),2) as 选课总数, round(ifnull(sum(sc.s_score),0),2) as 所选总课程 from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id;
SELECT st.s_id,st.s_name,COUNT(sc.c_id) "选课总数",SUM(CASE WHEN sc.s_score IS NULL THEN 0 ELSE sc.s_score END) "总成绩"
FROM student st
LEFT JOIN score sc ON st.s_id = sc.s_id
GROUP BY st.s_id;
-- 6、查询"李"姓老师的数量
-- sim 2020年2月1日16:00:35
select * from teacher th
where th.t_name like "李%";
-- sim errata 2020年2月1日16:02:42
select th.t_name, count(th.t_id) from teacher th
group by th.t_id having th.t_name like "李%";
SELECT t.t_name,COUNT(t.t_id) FROM teacher t
GROUP BY t.t_id HAVING t.t_name LIKE "李%";
-- 7、查询学过"张三"老师授课的同学的信息
-- sim error 2020年2月1日16:06:49
select distinct st.* from student st
left join score sc on st.s_id = sc.s_id
left join course cs on sc.c_id = cs.c_id
left join teacher th on cs.t_id = th.t_id and th.t_name = "张三";
-- sim errata 2020年2月1日16:08:30
select st.* from student st
left join score sc on st.s_id = sc.s_id
left join course cs on sc.c_id = cs.c_id
left join teacher th on cs.t_id = th.t_id
where th.t_name = "张三";
SELECT st.* FROM student st
LEFT JOIN score sc ON sc.s_id=st.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 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 st.* FROM student st WHERE st.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="张三")
);
-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
-- sim 2020年2月1日19:45:40
select * from student st
inner join score sc1 on st.s_id = sc1.s_id and sc1.c_id = "01"
inner join score sc2 on st.s_id = sc2.s_id and sc2.c_id = "02";
SELECT st.* FROM student st
INNER JOIN score sc ON sc.s_id = st.s_id
INNER JOIN course c ON c.c_id=sc.c_id AND c.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
INNER JOIN course c2 ON c2.c_id=sc2.c_id AND c2.c_id="02"
);
SELECT a.*
FROM
student a,
score b,
score c
WHERE
a.s_id = b.s_id
AND a.s_id = c.s_id
AND b.c_id = '01'
AND c.c_id = '02';
-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
-- sim 2020年2月1日20:23:01
select st.* from student st
inner join score sc on st.s_id = sc.s_id
inner join course cs on sc.c_id = cs.c_id and cs.c_id = "01"
where st.s_id not in (
select st2.s_id from student st2
inner join score sc2 on st2.s_id = sc2.s_id
inner join course cs2 on sc2.c_id = cs2.c_id and cs2.c_id = "02"
);
SELECT st.* FROM student st
INNER JOIN score sc ON sc.s_id = st.s_id
INNER JOIN course c ON c.c_id=sc.c_id AND c.c_id="01"
WHERE st.s_id NOT IN (
SELECT st2.s_id FROM student st2
INNER JOIN score sc2 ON sc2.s_id = st2.s_id
INNER JOIN course c2 ON c2.c_id=sc2.c_id AND c2.c_id="02"
);
-- 11、查询没有学全所有课程的同学的信息
-- sim error 2020年2月1日21:32:29 注:因为有人选课为null 所有要修改一下链接方式
select st.*, count(sc.c_id) as 科目数目, group_concat(sc.c_id) as 科目代码 from student st
inner join score sc on st.s_id = sc.s_id
group by st.s_id
having count(sc.c_id) < 3;
-- sim errata 2020年2月1日21:39:05 注:因为有人选课为null 所有要修改一下链接方式
select st.*, count(sc.c_id) as 科目数目, group_concat(sc.c_id) as 科目代码 from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id
having count(sc.c_id) < 3;
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 st1.s_id FROM student st1
INNER JOIN score sc2 ON sc2.s_id = st1.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"
));
SELECT a.*
FROM student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY
a.s_id
HAVING
COUNT(b.c_id) != '3';
-- 12、查询至少有一门课 与 学号为"01"的同学 所学相同的 同学的信息
-- sim error 2020年2月1日21:46:41 题意理解错误,当做了课程id 为 01
select * from student st
left join score sc on st.s_id = sc.s_id
where sc.c_id = "01";
-- sim errata 2020年2月1日21:49:42
select distinct st.* from student st
left join score sc on st.s_id = sc.s_id
where sc.c_id in (
select c_id from score where s_id = "01"
);
SELECT DISTINCT st.* FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
WHERE sc.c_id IN (
SELECT sc2.c_id FROM student st2
LEFT JOIN score sc2 ON sc2.s_id=st2.s_id
WHERE st2.s_id ='01'
);
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
-- sim error 2020年2月1日22:15:04
select st.*, group_concat(cs.c_name) from student st
left join score sc on st.s_id = sc.s_id
left join course cs on sc.c_id = cs.c_id
group by st.s_id
having group_concat(sc.c_id) = (
select group_concat(score.c_id) from student
left join score on student.s_id = score.s_id and student.s_id = "01");
-- sim errata todo 2020年2月2日08:29:37
select st.* from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id
having group_concat(sc.c_id) =
(
select group_concat(score.c_id) from student
left join score on student.s_id = score.s_id
where student.s_id = "01"
);
SELECT st.* FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY st.s_id
HAVING GROUP_CONCAT(sc.c_id) =
(
SELECT GROUP_CONCAT(sc2.c_id) FROM student st2
LEFT JOIN score sc2 ON sc2.s_id=st2.s_id
WHERE st2.s_id ='01'
);
-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
-- sim error 2020年2月2日08:36:07
select distinct st.* from student st
inner join score sc on st.s_id = sc.s_id
where sc.c_id not in (
select cs.c_id from course cs
inner join teacher th on cs.t_id = th.t_id
where th.t_name = "张三"
);
-- sim errata 2020年2月2日09:22:06
select st.s_name from student st
where st.s_id not in
(
select sc.s_id from score sc
inner join course cs on sc.c_id = cs.c_id
inner join teacher th on cs.t_id = th.t_id and th.t_name = "张三"
);
SELECT st.s_name FROM student st
WHERE st.s_id NOT IN (
SELECT sc.s_id FROM score sc
INNER JOIN course c ON c.c_id=sc.c_id
INNER JOIN teacher t ON t.t_id=c.t_id AND t.t_name="张三"
);
-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
-- sim error 2020年2月2日09:51:33
select st.*, round(sc.s_score,2) 平均分 from student st
inner join score sc on st.s_id = sc.s_id
group by st.s_id
having count(sc.s_score < 60) >= 2;
-- sim errata 照抄的 2020年2月2日10:16:32 todo 注: 加 todo 就是要看第二遍
select st.* , avg(sc1.s_score) 平均分 from student st
left join score sc1 on sc1.s_id = st.s_id
where sc1.s_id in (
select sc.s_id from score sc
where sc.s_score < 60 or sc.s_score is null
group by sc.s_id
having count(1) >= 2
)
group by st.s_id;
SELECT st.s_id,st.s_name,AVG(sc.s_score) FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
WHERE sc.s_id IN (
SELECT sc.s_id FROM score sc
WHERE sc.s_score<60 OR sc.s_score IS NULL
GROUP BY sc.s_id HAVING COUNT(1)>=2
)
GROUP BY st.s_id;
-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
-- sim error 2020年2月2日11:06:36
select st.* from student st, score sc1 where st.s_id = sc1.s_id and st.s_id in (
select sc.s_id from score sc where sc.c_id = "01" and sc.s_score < 60
)
group by st.s_id
order by sc1.s_score desc;
-- sim errata 2020年2月2日13:49:27
select * from student st
left join score sc on sc.s_id = st.s_id
where sc.c_id = "01" and sc.s_score < 60
order by sc.s_score desc;
SELECT st.*,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
WHERE sc.c_id="01" AND sc.s_score<60
ORDER BY sc.s_score DESC;
-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
-- sim 适当改良 2020年2月2日13:59:10
select st.s_id 学生id, st.s_name 学生姓名, ifnull(sc1.s_score,0) 语文, ifnull(sc2.s_score,0) 数学,
ifnull(sc3.s_score,0) 英语,ifnull(round(avg(sc4.s_score),2),0) 平均分 from student st
left join score sc1 on st.s_id = sc1.s_id and sc1.c_id = "01"
left join score sc2 on st.s_id = sc2.s_id and sc2.c_id = "02"
left join score sc3 on st.s_id = sc3.s_id and sc3.c_id = "03"
left join score sc4 on st.s_id = sc4.s_id
group by st.s_id
order by avg(sc4.s_score) desc;
SELECT st.s_id,st.s_name,AVG(sc4.s_score) "平均分",sc.s_score "语文",sc2.s_score "数学",sc3.s_score "英语" FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id="01"
LEFT JOIN score sc2 ON sc2.s_id=st.s_id AND sc2.c_id="02"
LEFT JOIN score sc3 ON sc3.s_id=st.s_id AND sc3.c_id="03"
LEFT JOIN score sc4 ON sc4.s_id=st.s_id
GROUP BY st.s_id
ORDER BY AVG(sc4.s_score) DESC;
SELECT st.s_id,st.s_name,
(CASE WHEN AVG(sc4.s_score) IS NULL THEN 0 ELSE AVG(sc4.s_score) END) "平均分",
(CASE WHEN sc.s_score IS NULL THEN 0 ELSE sc.s_score END) "语文",
(CASE WHEN sc2.s_score IS NULL THEN 0 ELSE sc2.s_score END) "数学",
(CASE WHEN sc3.s_score IS NULL THEN 0 ELSE sc3.s_score END) "英语"
FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id AND sc.c_id="01"
LEFT JOIN score sc2 ON sc2.s_id=st.s_id AND sc2.c_id="02"
LEFT JOIN score sc3 ON sc3.s_id=st.s_id AND sc3.c_id="03"
LEFT JOIN score sc4 ON sc4.s_id=st.s_id
GROUP BY st.s_id
ORDER BY AVG(sc4.s_score) DESC;
-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
-- sim error 2020年2月2日15:00:18 及格率 等不会计算 todo
select cs.c_id , cs.c_name, max(sc.s_score) 最高分, min(sc.s_score) 最低分, round(avg(sc.s_score),2) 平均分
from course cs
left join score sc on sc.c_id = cs.c_id
group by cs.c_id;
-- sim errata 2020年2月2日15:09:31 最高分最低分和平均分 只需要join一次score即可
select cs.*, max(sc1.s_score) 最高分, min(sc1.s_score) 最低分, avg(sc1.s_score) 平均分
, ((select count(s_id) from score where s_score >= 60 and c_id = cs.c_id)/ (select count(s_id) from score where c_id = cs.c_id )) 及格率
, ((select count(s_id) from score where s_score >= 70 and s_score < 80 and c_id = cs.c_id)/ (select count(s_id) from score where c_id = cs.c_id )) 中等率
, ((select count(s_id) from score where s_score >= 80 and s_score < 90 and c_id = cs.c_id)/ (select count(s_id) from score where c_id = cs.c_id )) 优良率
, ((select count(s_id) from score where s_score >= 90 and c_id = cs.c_id)/ (select count(s_id) from score where c_id = cs.c_id )) 优秀率
from course cs
left join score sc1 on sc1.c_id = cs.c_id
group by cs.c_id;
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、按各科成绩进行排序,并显示排名(实现不完全)
-- sim 函数 2020年2月2日15:36:54 todo
-- mysql没有rank函数
-- 加@score是为了防止用union all 后打乱了顺序
SELECT c1.s_id,c1.c_id,c1.c_name,@score:=c1.s_score,@i:=@i+1 FROM (SELECT c.c_name,sc.* FROM course c
LEFT JOIN score sc ON sc.c_id=c.c_id
WHERE c.c_id="01" ORDER BY sc.s_score DESC) c1 ,
(SELECT @i:=0) a
UNION ALL
SELECT c2.s_id,c2.c_id,c2.c_name,c2.s_score,@ii:=@ii+1 FROM (SELECT c.c_name,sc.* FROM course c
LEFT JOIN score sc ON sc.c_id=c.c_id
WHERE c.c_id="02" ORDER BY sc.s_score DESC) c2 ,
(SELECT @ii:=0) aa
UNION ALL
SELECT c3.s_id,c3.c_id,c3.c_name,c3.s_score,@iii:=@iii+1 FROM (SELECT c.c_name,sc.* FROM course c
LEFT JOIN score sc ON sc.c_id=c.c_id
WHERE c.c_id="03" ORDER BY sc.s_score DESC) c3;
SET @iii=0;
-- 20、查询学生的总成绩并进行排名
-- sim 2020年2月2日19:36:37
select st.* , (case when sum(sc.s_score) is null then 0 else sum(sc.s_score) end) 总分 from student st
left join score sc on st.s_id = sc.s_id
group by st.s_id
order by sum(sc.s_score) desc;
SELECT st.s_id,st.s_name
,(CASE WHEN SUM(sc.s_score) IS NULL THEN 0 ELSE SUM(sc.s_score) END)
FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY st.s_id ORDER BY SUM(sc.s_score) DESC;
-- 21、查询不同老师所教不同课程平均分从高到低显示
-- sim 2020年2月2日19:44:33
select cs.* , avg(sc.s_score) from course cs
left join score sc on cs.c_id = sc.c_id
group by cs.t_id, cs.c_id
order by avg(sc.s_score);
SELECT t.t_id,t.t_name,AVG(sc.s_score) FROM teacher t
LEFT JOIN course c ON c.t_id=t.t_id
LEFT JOIN score sc ON sc.c_id =c.c_id
GROUP BY t.t_id
ORDER BY AVG(sc.s_score) DESC;
-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
-- sim 2020年2月2日20:05:27 使用union 联合将多个结果拼接到一个结果里面
select a.* from (
select st.* ,sc.s_score from student st
left join score sc on st.s_id = sc.s_id
inner join course cs on sc.c_id = cs.c_id
where cs.c_id = "01"
order by sc.s_score desc limit 1,2) a
union all
select b.* from (
select st.* ,sc.s_score from student st
left join score sc on st.s_id = sc.s_id
inner join course cs on sc.c_id = cs.c_id
where cs.c_id = "01"
order by sc.s_score desc limit 1,2) b
union all
select c.* from (
select st.* ,sc.s_score from student st
left join score sc on st.s_id = sc.s_id
inner join course cs on sc.c_id = cs.c_id
where cs.c_id = "01"
order by sc.s_score desc limit 1,2) c;
SELECT a.* FROM (
SELECT st.*,c.c_id,c.c_name,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
INNER JOIN course c ON c.c_id =sc.c_id AND c.c_id="01"
ORDER BY sc.s_score DESC LIMIT 1,2 ) a
UNION ALL
SELECT b.* FROM (
SELECT st.*,c.c_id,c.c_name,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
INNER JOIN course c ON c.c_id =sc.c_id AND c.c_id="02"
ORDER BY sc.s_score DESC LIMIT 1,2) b
UNION ALL
SELECT c.* FROM (
SELECT st.*,c.c_id,c.c_name,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
INNER JOIN course c ON c.c_id =sc.c_id AND c.c_id="03"
ORDER BY sc.s_score DESC LIMIT 1,2) c
-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
-- sim ok
SELECT c.c_id,c.c_name
,((SELECT COUNT(1) FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score<=100 AND sc.s_score>80)/(SELECT COUNT(1) FROM score sc WHERE sc.c_id=c.c_id )) "100-85"
,((SELECT COUNT(1) FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score<=85 AND sc.s_score>70)/(SELECT COUNT(1) FROM score sc WHERE sc.c_id=c.c_id )) "85-70"
,((SELECT COUNT(1) FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score<=70 AND sc.s_score>60)/(SELECT COUNT(1) FROM score sc WHERE sc.c_id=c.c_id )) "70-60"
,((SELECT COUNT(1) FROM score sc WHERE sc.c_id=c.c_id AND sc.s_score<=60 AND sc.s_score>=0)/(SELECT COUNT(1) FROM score sc WHERE sc.c_id=c.c_id )) "60-0"
FROM course c ORDER BY c.c_id
-- 24、查询学生平均成绩及其名次
-- sim 函数 todo
SET @i=0;
SELECT a.*,@i:=@i+1 FROM (
SELECT st.s_id,st.s_name,ROUND((CASE WHEN AVG(sc.s_score) IS NULL THEN 0 ELSE AVG(sc.s_score) END),2) "平均分" FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY st.s_id ORDER BY sc.s_score DESC) a;
-- 25、查询各科成绩前三名的记录
-- sim 2020年2月2日21:19:41
select a.* from (
select st.* , sc.s_score, cs.c_name from student st
left join score sc on sc.s_id = st.s_id
inner join course cs on cs.c_id = sc.c_id and cs.c_id = '01'
order by sc.s_score desc limit 0,3) a
union all
select b.* from (
select st.* , sc.s_score, cs.c_name from student st
left join score sc on sc.s_id = st.s_id
inner join course cs on cs.c_id = sc.c_id and cs.c_id = '02'
order by sc.s_score desc limit 0,3) b
union all
select c.* from (
select st.* , sc.s_score, cs.c_name from student st
left join score sc on sc.s_id = st.s_id
inner join course cs on cs.c_id = sc.c_id and cs.c_id = '03'
order by sc.s_score desc limit 0,3) c;
SELECT a.* FROM (
SELECT st.s_id,st.s_name,c.c_id,c.c_name,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
INNER JOIN course c ON c.c_id=sc.c_id AND c.c_id='01'
ORDER BY sc.s_score DESC LIMIT 0,3) a
UNION ALL
SELECT b.* FROM (
SELECT st.s_id,st.s_name,c.c_id,c.c_name,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
INNER JOIN course c ON c.c_id=sc.c_id AND c.c_id='02'
ORDER BY sc.s_score DESC LIMIT 0,3) b
UNION ALL
SELECT c.* FROM (
SELECT st.s_id,st.s_name,c.c_id,c.c_name,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
INNER JOIN course c ON c.c_id=sc.c_id AND c.c_id='03'
ORDER BY sc.s_score DESC LIMIT 0,3) c;
-- 26、查询每门课程被选修的学生数
-- sim error 2020年2月2日22:23:02
select *, count(cs.c_id), cs.c_name from course cs
inner join score sc on cs.c_id = sc.c_id
group by cs.c_id;
-- sim errata todo 2020年2月4日09:51:01
select cs.c_id, cs.c_name, count(cs.c_id) from course cs
left join score sc on cs.c_id = sc.c_id
inner join student st on st.s_id = sc.s_id
group by cs.c_id;
-- error 例题的答案错误
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 st.* from student st
left join score sc on st.s_id = sc.s_id
join course cs on cs.c_id = sc.c_id
group by st.s_id
having count(cs.c_id) = 2;
SELECT st.s_id,st.s_name FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
INNER JOIN course c ON c.c_id=sc.c_id
GROUP BY st.s_id HAVING COUNT(1)=2;
-- 28、查询男生、女生人数
-- sim 2020年2月4日10:15:02
select st.s_sex, count(1) from student st group by st.s_sex;
SELECT st.s_sex,COUNT(1) FROM student st GROUP BY st.s_sex;
-- 29、查询名字中含有"风"字的学生信息
-- sim 2020年2月4日10:16:07
select st.* from student st where st.s_name like "%风%";
SELECT st.* FROM student st WHERE st.s_name LIKE "%风%";
-- 30、查询同名同性学生名单,并统计同名人数
-- sim error 2020年2月4日10:18:59
select * from student st1, student st2
where st1.s_name = st2.s_name and st1.s_sex = st2.s_sex
group by st1.s_id;
-- sim errata sql mode 无法设置 2020年2月4日10:26:39
set @@GLOBAL.sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION;
select st.* , count(1) from student st group by st.s_name, st.s_sex having count(1) > 1;
SELECT st.*,COUNT(1) FROM student st GROUP BY st.s_name,st.s_sex HAVING COUNT(1)>1;
-- 31、查询1990年出生的学生名单
SELECT st.* FROM student st WHERE st.s_birth LIKE "1990%";
-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
-- sim 2020年2月4日13:06:18
select cs.c_id, avg(sc.s_score) from course cs
left join score sc on cs.c_id = sc.c_id
group by cs.c_id
order by avg(sc.s_score) desc, cs.c_id asc;
SELECT c.c_id,c.c_name,AVG(sc.s_score) FROM course c
INNER JOIN score sc ON sc.c_id=c.c_id
GROUP BY c.c_id ORDER BY AVG(sc.s_score) DESC,c.c_id ASC;
-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
-- sim 2020年2月4日13:41:03
select st.s_id, st.s_name, avg(sc.s_score) 平均分 from student st
inner join score sc on st.s_id = sc.s_id
group by st.s_id
having avg(sc.s_score) > 85;
-- sim 穿插再复习一下链接
-- https://www.cnblogs.com/mafeng/p/10150013.html
create database testdb;
use testdb;
CREATE TABLE t1 (
id INT PRIMARY KEY,
pattern VARCHAR(50) NOT NULL
);
CREATE TABLE t2 (
id VARCHAR(50) PRIMARY KEY,
pattern VARCHAR(50) NOT NULL
);
INSERT INTO t1(id, pattern)
VALUES(1,'Divot'),
(2,'Brick'),
(3,'Grid');
INSERT INTO t2(id, pattern)
VALUES('A','Brick'),
('B','Grid'),
('C','Diamond');
select t1.id, t2.id
from t1
cross join t2;
select t1.id, t2.id
from t1 inner join t2
on t1.pattern = t2.pattern;
select t1.id, t2.id
from t1 left join t2
on t1.pattern = t2.pattern;
select t1.id, t2.id
from t1 right join t2
on t1.pattern = t2.pattern;
SELECT st.s_id,st.s_name,AVG(sc.s_score) FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY st.s_id HAVING AVG(sc.s_score)>=85;
-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
SELECT st.s_id,st.s_name,sc.s_score FROM student st
INNER JOIN score sc ON sc.s_id=st.s_id AND sc.s_score<60
INNER JOIN course c ON c.c_id=sc.c_id AND c.c_name ="数学" ;
-- 35、查询所有学生的课程及分数情况;
SELECT st.s_id,st.s_name,c.c_name,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
LEFT JOIN course c ON c.c_id =sc.c_id
ORDER BY st.s_id,c.c_name;
-- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
-- sim todo 第一遍没做出来 2020年2月4日14:35:37
select st.* , cs.c_name, sc.s_score from student st
join score sc on st.s_id = sc.s_id
join course cs on cs.c_id = sc.c_id
where st.s_id in (
select st2.s_id from student st2
join score sc2 on st2.s_id = sc2.s_id
where sc2.s_score > 70
);
SELECT st2.s_id,st2.s_name,c2.c_name,sc2.s_score FROM student st2
LEFT JOIN score sc2 ON sc2.s_id=st2.s_id
LEFT JOIN course c2 ON c2.c_id=sc2.c_id
WHERE st2.s_id IN(
SELECT st.s_id FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY st.s_id HAVING MIN(sc.s_score)>=70)
ORDER BY s_id;
-- 37、查询不及格的课程
SELECT st.s_id,c.c_name,st.s_name,sc.s_score FROM student st
INNER JOIN score sc ON sc.s_id=st.s_id AND sc.s_score<60
INNER JOIN course c ON c.c_id=sc.c_id ;
-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
-- sim 2020年2月4日14:37:32
select st.* from student st
inner join score sc on sc.s_id = st.s_id
where sc.c_id = "01" and sc.s_score >= 80;
SELECT st.s_id,st.s_name,sc.s_score FROM student st
INNER JOIN score sc ON sc.s_id=st.s_id AND sc.c_id="01" AND sc.s_score>=80;
-- 39、求每门课程的学生人数
-- sim 2020年2月4日14:40:05
select cs.*, count(st.s_id) from course cs
left join score sc on cs.c_id = sc.c_id
left join student st on sc.s_id = st.s_id
group by cs.c_id;
SELECT c.c_id,c.c_name,COUNT(1) FROM course c
INNER JOIN score sc ON sc.c_id=c.c_id
GROUP BY c.c_id;
-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
-- sim
select st.*, cs.c_name, th.t_name from student st
left join score sc on st.s_id = sc.s_id
left join course cs on cs.c_id = sc.c_id
join teacher th on th.t_id = cs.t_id and th.t_name = "张三"
order by sc.s_score desc
limit 0,1;
SELECT st.*,c.c_name,sc.s_score,t.t_name FROM student st
INNER JOIN score sc ON sc.s_id=st.s_id
INNER JOIN course c ON c.c_id=sc.c_id
INNER JOIN teacher t ON t.t_id=c.t_id AND t.t_name="张三"
ORDER BY sc.s_score DESC
LIMIT 0,1;
-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
-- sim todo
select st.* from student st
join score sc on st.s_id = sc.s_id
join course cs on cs.c_id = sc.c_id;
SELECT st.s_id,st.s_name,sc.c_id,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
LEFT JOIN course c ON c.c_id=sc.c_id
WHERE (
SELECT COUNT(1) FROM student st2
LEFT JOIN score sc2 ON sc2.s_id=st2.s_id
LEFT JOIN course c2 ON c2.c_id=sc2.c_id
WHERE sc.s_score=sc2.s_score AND c.c_id!=c2.c_id
)>1;
-- 42、查询每门功成绩最好的前两名
SELECT a.* FROM (SELECT st.s_id,st.s_name,c.c_name,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
INNER JOIN course c ON c.c_id=sc.c_id AND c.c_id="01"
ORDER BY sc.s_score DESC LIMIT 0,2) a
UNION ALL
SELECT b.* FROM (SELECT st.s_id,st.s_name,c.c_name,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
INNER JOIN course c ON c.c_id=sc.c_id AND c.c_id="02"
ORDER BY sc.s_score DESC LIMIT 0,2) b
UNION ALL
SELECT c.* FROM (SELECT st.s_id,st.s_name,c.c_name,sc.s_score FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
INNER JOIN course c ON c.c_id=sc.c_id AND c.c_id="03"
ORDER BY sc.s_score DESC LIMIT 0,2) c;
-- sim todo 借鉴(更准确,漂亮):
SELECT a.s_id,a.c_id,a.s_score FROM score a
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;
-- sim todo 2020年2月4日20:50:38
select sc1.s_id, sc1.c_id, sc1.s_score from score sc1
where (select count(1) from score sc2 where sc2.c_id = sc1.c_id and sc2.s_score >= sc1.s_score) <= 2
order by sc1.c_id;
-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,
-- 若人数相同,按课程号升序排列
-- sim 2020年2月4日20:56:03
select cs.c_name, count(st.s_id) from course cs
left join score sc on cs.c_id = sc.c_id
inner join student st on st.s_id = sc.s_id
group by cs.c_id
having count(st.s_id) > 5
order by count(st.s_id) desc, cs.c_id asc;
SELECT sc.c_id,COUNT(1) FROM score sc
LEFT JOIN course c ON c.c_id=sc.c_id
GROUP BY c.c_id HAVING COUNT(1)>5
ORDER BY COUNT(1) DESC,sc.c_id ASC;
-- 44、检索至少选修两门课程的学生学号
SELECT st.s_id FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY st.s_id HAVING COUNT(1)>=2;
-- 45、查询选修了全部课程的学生信息
SELECT st.* FROM student st
LEFT JOIN score sc ON sc.s_id=st.s_id
GROUP BY st.s_id HAVING COUNT(1)=(SELECT COUNT(1) FROM course);
-- 46、查询各学生的年龄
SELECT st.*,TIMESTAMPDIFF(YEAR,st.s_birth,NOW()) FROM student st;
-- 47、查询本周过生日的学生
-- 此处可能有问题,week函数取的为当前年的第几周,2017-12-12是第50周而2018-12-12是第49周,可以取月份,day,星期几(%w),
-- 再判断本周是否会持续到下一个月进行判断,太麻烦,不会写
SELECT st.* FROM student st
WHERE WEEK(NOW())=WEEK(DATE_FORMAT(st.s_birth,'%Y%m%d'));
-- 48、查询下周过生日的学生
SELECT st.* FROM student st
WHERE WEEK(NOW())+1=WEEK(DATE_FORMAT(st.s_birth,'%Y%m%d'));
-- 49、查询本月过生日的学生
SELECT st.* FROM student st
WHERE MONTH(NOW())=MONTH(DATE_FORMAT(st.s_birth,'%Y%m%d'));
-- 50、查询下月过生日的学生
-- 注意:当 当前月为12时,用month(now())+1为13而不是1,可用timestampadd()函数或mod取模
SELECT st.* FROM student st
WHERE MONTH(TIMESTAMPADD(MONTH,1,NOW()))=MONTH(DATE_FORMAT(st.s_birth,'%Y%m%d'));
-- 或
SELECT st.* FROM student st WHERE (MONTH(NOW()) + 1) MOD 12 = MONTH(DATE_FORMAT(st.s_birth,'%Y%m%d'));