表简介
--1.学生表
Student(SId,Sname,Sage,Ssex)
--SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2.课程表
Course(CId,Cname,TId)
--CId 课程编号,Cname 课程名称,TId 教师编号
--3.教师表
Teacher(TId,Tname)
--TId 教师编号,Tname 教师姓名
--4.成绩表
SC(SId,CId,score)
--SId 学生编号,CId 课程编号,score 分数
创表语句 以及表数据的添加 在最下面
表数据显示
#学生表
mysql> select *from Student;
+------+--------+---------------------+------+
| SID | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
| 09 | 张三 | 2017-12-20 00:00:00 | 女 |
| 10 | 李四 | 2017-12-25 00:00:00 | 女 |
| 11 | 李四 | 2012-06-06 00:00:00 | 女 |
| 12 | 赵六 | 2013-06-13 00:00:00 | 女 |
| 13 | 孙七 | 2014-06-01 00:00:00 | 女 |
+------+--------+---------------------+------+
12 rows in set (0.00 sec)
#课程表
mysql> select *from Course;
+------+--------+------+
| CId | Cname | TId |
+------+--------+------+
| 01 | 语文 | 02 |
| 02 | 数学 | 01 |
| 03 | 英语 | 03 |
+------+--------+------+
3 rows in set (0.00 sec)
#教师表
mysql> select *from Teacher;
+------+--------+
| TId | Tname |
+------+--------+
| 01 | 张三 |
| 02 | 李四 |
| 03 | 王五 |
+------+--------+
#成绩表
mysql> select *from SC;
+------+------+-------+
| SId | CId | score |
+------+------+-------+
| 01 | 01 | 80.0 |
| 01 | 02 | 90.0 |
| 01 | 03 | 99.0 |
| 02 | 01 | 70.0 |
| 02 | 02 | 60.0 |
| 02 | 03 | 80.0 |
| 03 | 01 | 80.0 |
| 03 | 02 | 80.0 |
| 03 | 03 | 80.0 |
| 04 | 01 | 50.0 |
| 04 | 02 | 30.0 |
| 04 | 03 | 20.0 |
| 05 | 01 | 76.0 |
| 05 | 02 | 87.0 |
| 06 | 01 | 31.0 |
| 06 | 03 | 34.0 |
| 07 | 02 | 89.0 |
| 07 | 03 | 98.0 |
+------+------+-------+
18 rows in set (0.00 sec)
练习题目
1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
---> 分析 凡是设计同行比较时都需要使用 join
---> 01 比 02 课程成绩高
FROM SC s1 jion SC s2 on s1.SId = s2.SId AND s1.CId = '01' AND s2.CID = '02 where s1.score > s2.score';
-->结果如下
mysql> select * from SC s1 join SC s2 on s1.SId = s2.SId AND s1.CId = '01' AND s2.CID = '02' where s1.score > s2.score; +------+------+-------+------+------+-------+
| SId | CId | score | SId | CId | score |
+------+------+-------+------+------+-------+
| 02 | 01 | 70.0 | 02 | 02 | 60.0 |
| 04 | 01 | 50.0 | 04 | 02 | 30.0 |
+------+------+-------+------+------+-------+
2 rows in set (0.00 sec)
--->学生信息 需要把学生表(Student) 在进行 join
select * from SC s1 join SC s2 on s1.SId = s2.SId AND s1.CId = '01' AND s2.CID = '02'join Student st on st.SId = s1.SId where s1.score > s2.score;
mysql> select * from SC s1 join SC s2 on s1.SId = s2.SId AND s1.CId = '01' AND s2.CID = '02'join Student st on st.SId = s1.SId where s1.score > s2.score;
+------+------+-------+------+------+-------+------+--------+---------------------+------+
| SId | CId | score | SId | CId | score | SID | Sname | Sage | Ssex |
+------+------+-------+------+------+-------+------+--------+---------------------+------+
| 02 | 01 | 70.0 | 02 | 02 | 60.0 | 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 04 | 01 | 50.0 | 04 | 02 | 30.0 | 04 | 李云 | 1990-12-06 00:00:00 | 男 |
+------+------+-------+------+------+-------+------+--------+---------------------+------+
2 rows in set (0.00 sec)
---> 最终结果:
mysql> select st.Sname,st.Ssex,s1.score as '01课程分数',s2.score as '02课程分数' from SC s1 join SC s2 on s1.SId = s2.SId AND s1.CId = '01' AND s2.CID = '02'join Student st on st.SId = s1.SId where s1.score > s2.score;
+--------+------+----------------+----------------+
| Sname | Ssex | 01课程分数 | 02课程分数 |
+--------+------+----------------+----------------+
| 钱电 | 男 | 70.0 | 60.0 |
| 李云 | 男 | 50.0 | 30.0 |
+--------+------+----------------+----------------+
2 rows in set (0.00 sec)
----> 另一种解法
mysql> select stu.SId,stu.Sname,s.score
-> from student as stu
-> right join (
-> select s1.SId,s1.score
-> from
-> (select SId,score from SC where CId = '01') s1
-> join
-> (select SId,score from SC where CId = '02') s2
-> on s1.SId = s2.SId
-> where s1.score > s2.score
-> ) as s
-> on stu.SId = s.SId;
+------+--------+-------+
| SId | Sname | score |
+------+--------+-------+
| 02 | 钱电 | 70.0 |
| 04 | 李云 | 50.0 |
+------+--------+-------+
2 rows in set (0.00 sec)
2. 查询同时选择" 01 "课程和" 02 "课程的情况学生信息
---> 解析 此时还需要对同一个表进行比较 还需要使用join
from SC s1 join SC s2 on s1.SId = S2.SId AND s1.CId = '01' AND s2.CId ='02' join Student st on s1.SId = st.SId;
---> 最终结果:
mysql> select st.SId, st.Sname ,st.Ssex from SC s1 join SC s2 on s1.SId = S2.SId AND s1.CId = '01' AND s2.CId ='02' join Student st on s1.SId = st.SId;
+------+--------+------+
| SId | Sname | Ssex |
+------+--------+------+
| 01 | 赵雷 | 男 |
| 02 | 钱电 | 男 |
| 03 | 孙风 | 男 |
| 04 | 李云 | 男 |
| 05 | 周梅 | 女 |
+------+--------+------+
5 rows in set (0.00 sec)
3. 查询平均成绩大于等于60分的同学的学生编号 学生姓名 以及平均分
---> 解析 凡是遇到 每一 字眼的时候 往往需要使用group by
from SC s1 join Student st on s1.SId = st.SId group by s1.SId
having AVG(score) >60;
注意: mysql 5.7 之后 select后面的字段 必须是group by 里的字段 否则就会报错 但不包含聚合函数 要想添加group by之外的字段使用 any_value()
---> 最终结果:
mysql> select st.* from SC s1 join SC s2 on s1.SId = S2.SId AND s1.CId = '01' AND s2.CId ='02' join Student st on s1.SId = st.SId;
+------+--------+---------------------+------+
| SID | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
+------+--------+---------------------+------+
5 rows in set (0.00 sec)
---> 子查询的解法:
mysql> select *from Student where SId in (select s1.SId from SC s1 join SC s2 on s1.SId = S2.SId AND s1.CId = '01' AND s2.CId ='02');
+------+--------+---------------------+------+
| SID | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
+------+--------+---------------------+------+
5 rows in set (0.00 sec)
6.查询在 SC 表存在成绩的学生信息
---> 解析说明 子查询有去重功能 而 join没有去重功能
$\color{red}{红色字}$
---> 解法一: 子查询
select * from Student where SId in (select SId from SC);
--->最终结果:
mysql> select * from Student where SId in (select SId from SC);
+------+--------+---------------------+------+
| SID | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
+------+--------+---------------------+------+
7 rows in set (0.00 sec)
---> 解法二: join
from SC s1 join Student st on s1.SId = st.SId;
-->显示结果 有重复需要 distinct
mysql> select st.* from SC s1 join Student st on s1.SId = st.SId;
+------+--------+---------------------+------+
| SID | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
+------+--------+---------------------+------+
18 rows in set (0.00 sec)
----> 最终结果:
mysql> select distinct st.* from SC s1 join Student st on s1.SId = st.SId;
+------+--------+---------------------+------+
| SID | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
+------+--------+---------------------+------+
7 rows in set (0.00 sec)
7.查询所有同学的学生编号、学生姓名、选课总数、平均成绩,所有课程的总成绩(没成绩的显示为 null )
---> 解析 一般有null显示的都是使用 左连接 left join
---> 第一步学生表(Student) 和 成绩表(SC)进行连接
mysql> select * from Student st left join SC s1 on st.SId = s1.SId;
+------+--------+---------------------+------+------+------+-------+
| SID | Sname | Sage | Ssex | SId | CId | score |
+------+--------+---------------------+------+------+------+-------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 01 | 01 | 80.0 |
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 01 | 02 | 90.0 |
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 01 | 03 | 99.0 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 02 | 01 | 70.0 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 02 | 02 | 60.0 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 02 | 03 | 80.0 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 | 03 | 01 | 80.0 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 | 03 | 02 | 80.0 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 | 03 | 03 | 80.0 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 04 | 01 | 50.0 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 04 | 02 | 30.0 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 04 | 03 | 20.0 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 | 05 | 01 | 76.0 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 | 05 | 02 | 87.0 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 | 06 | 01 | 31.0 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 | 06 | 03 | 34.0 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 | 07 | 02 | 89.0 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 | 07 | 03 | 98.0 |
| 09 | 张三 | 2017-12-20 00:00:00 | 女 | NULL | NULL | NULL |
| 10 | 李四 | 2017-12-25 00:00:00 | 女 | NULL | NULL | NULL |
| 11 | 李四 | 2012-06-06 00:00:00 | 女 | NULL | NULL | NULL |
| 12 | 赵六 | 2013-06-13 00:00:00 | 女 | NULL | NULL | NULL |
| 13 | 孙七 | 2014-06-01 00:00:00 | 女 | NULL | NULL | NULL |
+------+--------+---------------------+------+------+------+-------+
---> 第二步对表 以学生的 SId 进行分组
mysql> select st.SId, count(1) from Student st left join SC s1 on st.SId = s1.SId group by st.SId ;
+------+----------+
| SId | count(1) |
+------+----------+
| 01 | 3 |
| 02 | 3 |
| 03 | 3 |
| 04 | 3 |
| 05 | 2 |
| 06 | 2 |
| 07 | 2 |
| 09 | 1 |
| 10 | 1 |
| 11 | 1 |
| 12 | 1 |
| 13 | 1 |
+------+----------+
--->第三步进行 所需字段显示
--->最终结果:
mysql> select st.SId,any_value(st.Sname) Sname , count(1),AVG(score),SUM(score) from Student st left join SC s1 on st.SId = s1.SId group by st.SId ;
+------+--------+----------+------------+------------+
| SId | Sname | count(1) | AVG(score) | SUM(score) |
+------+--------+----------+------------+------------+
| 01 | 赵雷 | 3 | 89.66667 | 269.0 |
| 02 | 钱电 | 3 | 70.00000 | 210.0 |
| 03 | 孙风 | 3 | 80.00000 | 240.0 |
| 04 | 李云 | 3 | 33.33333 | 100.0 |
| 05 | 周梅 | 2 | 81.50000 | 163.0 |
| 06 | 吴兰 | 2 | 32.50000 | 65.0 |
| 07 | 郑竹 | 2 | 93.50000 | 187.0 |
| 09 | 张三 | 1 | NULL | NULL |
| 10 | 李四 | 1 | NULL | NULL |
| 11 | 李四 | 1 | NULL | NULL |
| 12 | 赵六 | 1 | NULL | NULL |
| 13 | 孙七 | 1 | NULL | NULL |
+------+--------+----------+------------+------------+
12 rows in set (0.00 sec)
8.查询「李」姓老师的数量
mysql> select any_value(Tname) Tname , count(1) from Teacher where Tname like '李%';
+--------+----------+
| Tname | count(1) |
+--------+----------+
| 李四 | 1 |
+--------+----------+
1 row in set (0.00 sec)
9.查询学过「张三」老师授课的同学的信息
---> 解析 因为老师表 只和课程表有关联 课程表只和成绩表有关联 再有成绩表关联学生表 所以此时需要把四个表进行join连接
select st.*
from Student st join SC s1 on st.SId = s1.SId
join Course Cu on s1.CId =Cu.CId
join Teacher t on Cu.TId = t.TId
where t.Tname = "张三";
---> 最终结果:
mysql> select st.*
-> from Student st join SC s1 on st.SId = s1.SId
-> join Course Cu on s1.CId =Cu.CId
-> join Teacher t on Cu.TId = t.TId
-> where t.Tname = "张三";
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
+------+--------+---------------------+------+
6 rows in set (0.00 sec)
10.查询没有学全所有课程的同学的信息
--->解析 先查出学完课程学生的SId 利用 not in在进行排除即可
--->学生学习课程的个数等于 课程的总个数 就是学全部课程的学生
课程总数 select count(1) from Course cu group by cu.CId
学生学习的课程数 select count(1) from SC group by SId
select SId from SC group by SId having count(1) = (select count(1) from Course);
mysql> select SId from SC group by SId having count(1) = (select count(1) from Course);
+------+
| SId |
+------+
| 01 |
| 02 |
| 03 |
| 04 |
+------+
4 rows in set (0.00 sec)
---> 最终结果:
mysql> select * from Student where SId not in (select SId from SC group by SId having count(1) = (select count(1) from Course));
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
| 09 | 张三 | 2017-12-20 00:00:00 | 女 |
| 10 | 李四 | 2017-12-25 00:00:00 | 女 |
| 11 | 李四 | 2012-06-06 00:00:00 | 女 |
| 12 | 赵六 | 2013-06-13 00:00:00 | 女 |
| 13 | 孙七 | 2014-06-01 00:00:00 | 女 |
+------+--------+---------------------+------+
8 rows in set (0.00 sec)
----> 另一种解题思路 left join
----> 结果显示:
mysql> select st.SId,any_value(st.Sname) Sname,any_value(Sage) Sage,any_value(Ssex) Ssex from Student st left join SC s1 on st.SId = s1.SId group by st.SId having count(1) < (select count(1) from Course);
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
| 09 | 张三 | 2017-12-20 00:00:00 | 女 |
| 10 | 李四 | 2017-12-25 00:00:00 | 女 |
| 11 | 李四 | 2012-06-06 00:00:00 | 女 |
| 12 | 赵六 | 2013-06-13 00:00:00 | 女 |
| 13 | 孙七 | 2014-06-01 00:00:00 | 女 |
+------+--------+---------------------+------+
8 rows in set (0.00 sec)
11.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
--->解析: 查找 '01' 同学所学的全部课程
select CId from SC where SId = '01'
mysql> select CId from SC where SId = '01';
+------+
| CId |
+------+
| 01 |
| 02 |
| 03 |
+------+
---> 查找所有学生所学的课程
from Student st join SC s1 on st.SId = s1.SId where s1.CId in (select CId from SC where SId = '01');
mysql> select * from Student st join SC s1 on st.SId = s1.SId;
+------+--------+---------------------+------+------+------+-------+
| SId | Sname | Sage | Ssex | SId | CId | score |
+------+--------+---------------------+------+------+------+-------+
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 01 | 01 | 80.0 |
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 01 | 02 | 90.0 |
| 01 | 赵雷 | 1990-01-01 00:00:00 | 男 | 01 | 03 | 99.0 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 02 | 01 | 70.0 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 02 | 02 | 60.0 |
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 | 02 | 03 | 80.0 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 | 03 | 01 | 80.0 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 | 03 | 02 | 80.0 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 | 03 | 03 | 80.0 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 04 | 01 | 50.0 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 04 | 02 | 30.0 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 | 04 | 03 | 20.0 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 | 05 | 01 | 76.0 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 | 05 | 02 | 87.0 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 | 06 | 01 | 31.0 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 | 06 | 03 | 34.0 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 | 07 | 02 | 89.0 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 | 07 | 03 | 98.0 |
+------+--------+---------------------+------+------+------+-------+
18 rows in set (0.01 sec)
----> 最终结果: st.SId != '01' 去除01同学的信息
mysql> select distinct st.* from Student st join SC s1 on st.SId = s1.SId where s1.CId in (select CId from SC where SId = '01') AND st.SId != '01';
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
+------+--------+---------------------+------+
6 rows in set (0.00 sec)
----> 子查询的解析方式
#所有01的课程
select CId from SC where SId = '01'
#有一门课程和 '01'学生课程相同的学生 SId
select SId from SC where CId in (select CId from SC where SId = '01')
# 所有包含 SId的学生信息
select * from Student where SId in (select SId from SC where CId in (select CId from SC where SId = '01')) AND SId != '01';
---> 最终结果:
mysql> select * from Student where SId in (select SId from SC where CId in (select CId from SC where SId = '01')) AND SId != '01';
+------+--------+---------------------+------+
| SId | Sname | Sage | Ssex |
+------+--------+---------------------+------+
| 02 | 钱电 | 1990-12-21 00:00:00 | 男 |
| 03 | 孙风 | 1990-12-20 00:00:00 | 男 |
| 04 | 李云 | 1990-12-06 00:00:00 | 男 |
| 05 | 周梅 | 1991-12-01 00:00:00 | 女 |
| 06 | 吴兰 | 1992-01-01 00:00:00 | 女 |
| 07 | 郑竹 | 1989-01-01 00:00:00 | 女 |
+------+--------+---------------------+------+
6 rows in set (0.00 sec)
12.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
未解决
13.查询没学过"张三"老师讲授的任一门课程的学生姓名
14.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
15.检索 '01'课程分数小于60,按分数降序列的学生信息
16.按平均成绩从高到底显示所有学生的所有课程的成绩以及平均成绩
17.查询各科成绩最高分 最低分 和平均分:
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
18.按各科平均成绩进行排序,并显示排名,Score重复时保留名字空缺
19.按各科平均成绩进行排序,并显示排名,Score 重复是不保留名次空缺
20.查询学生的总成绩,并进行排名, 总分重复时保留名次空缺
21.查询学生的总成绩,并进行排名,总分重复是不保留名次空缺
22.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0]及所占百分比
23.查询各科成绩前三名的记录
24.查询每门课程被选修的学生数
25.查询只选修两门课程的学生学号和姓名
26.查询男生 女生人数
27.查询名字中含'风'字的学生信息
28.查询同名同性学生名单,并统计同名人数
29.查询 1990 年出生的学生名单
30.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
31.查询平均成绩大于等于85的所有学生的学号,姓名和平均成绩
32.查询课程名称为'数学',且分数低于60的学生和分数
33.查询所有学生的课程以及分数情况(存在学生没成绩,没选课的情侣)
34.查询任何一门课程成绩在70分以上的姓名,课程名称和分数
35.查询不及格的课程
36.查询课程编号为'01' 且课程成绩在80分以上的学生的学号和姓名
37.求每门课程的学生人数
38.成绩不重复,查询选修'张三'老师所受学生中,成绩最高的学生信息以及成绩
39.成绩有重复的情况下,查询选修'张三'老师所受课程的学生中,成绩最高的学生信息以及成绩
40.查询不同课程成绩相同的学生的学生编号,课程编号,学生成绩
41.查询每门课程成绩最好的前两名
42.统计每门课程的学生选修人数(超过5人的课程统计)
43.检索至少选修两门课程的学生学号
44.查询选修了全部课程的学生信息
45.查询个学生的年龄,按年份来算
46.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
TIMESTAMPDIFF() 从日期时间表达式中减去间隔
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
47.查询本周过生日的学生
48.查询下周过生日的学生
49.查询本月过生日的学生
50.查询下月过生日的学生
创表语句 以及表数据的添加
--学生表 Student
create table Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-12-20' , '男');
insert into Student values('04' , '李云' , '1990-12-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-01-01' , '女');
insert into Student values('07' , '郑竹' , '1989-01-01' , '女');
insert into Student values('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2012-06-06' , '女');
insert into Student values('12' , '赵六' , '2013-06-13' , '女');
insert into Student values('13' , '孙七' , '2014-06-01' , '女');
-- 科目表 Course
create table Course(CId varchar(10),Cname varchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
-- 教师表 Teacher
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
-- 成绩表 SC
create table SC(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);