三表关联查询
查询所有学生的 name
、课程名 ( course
表中的 name
) 和 degree
。
只有score表中关联学生的学号和课堂号,我们只要围绕着score这张表查询就好了。
SELECT * FROM score; +------+-------+--------+ | s_no | c_no | degree | +------+-------+--------+ | 103 | 3-105 | 92 | | 103 | 3-245 | 86 | | 103 | 6-166 | 85 | | 105 | 3-105 | 88 | | 105 | 3-245 | 75 | | 105 | 6-166 | 79 | | 109 | 3-105 | 76 | | 109 | 3-245 | 68 | | 109 | 6-166 | 81 | +------+-------+--------+
只要把 s_no 和 c_no 替换成 student 和 course 表中对应的 name 字段值就好了。
首先把 s_no 替换成 student 表中的 name 字段:
SELECT name, c_no, degree FROM student, score WHERE student.no = score.s_no; +-----------+-------+--------+ | name | c_no | degree | +-----------+-------+--------+ | 王丽 | 3-105 | 92 | | 王丽 | 3-245 | 86 | | 王丽 | 6-166 | 85 | | 王芳 | 3-105 | 88 | | 王芳 | 3-245 | 75 | | 王芳 | 6-166 | 79 | | 赵铁柱 | 3-105 | 76 | | 赵铁柱 | 3-245 | 68 | | 赵铁柱 | 6-166 | 81 | +-----------+-------+--------+
再把c_no替换成course表中的name字段:
-- 课程表 SELECT no, name FROM course; +-------+-----------------+ | no | name | +-------+-----------------+ | 3-105 | 计算机导论 | | 3-245 | 操作系统 | | 6-166 | 数字电路 | | 9-888 | 高等数学 | +-------+-----------------+ -- 由于字段名存在重复,使用 "表名.字段名 as 别名" 代替。 SELECT student.name as s_name, course.name as c_name, degree FROM student, score, course WHERE student.NO = score.s_no AND score.c_no = course.no;
子查询加分组求平均分
查询95031
班学生每门课程的平均成绩。
在score表中根据student表的学生编号筛选出学生的课堂号和成绩:
-- IN (..): 将筛选出的学生号当做 s_no 的条件查询 SELECT s_no, c_no, degree FROM score WHERE s_no IN (SELECT no FROM student WHERE class = '95031'); +------+-------+--------+ | s_no | c_no | degree | +------+-------+--------+ | 105 | 3-105 | 88 | | 105 | 3-245 | 75 | | 105 | 6-166 | 79 | | 109 | 3-105 | 76 | | 109 | 3-245 | 68 | | 109 | 6-166 | 81 | +------+-------+--------+
这时只要将c_no分组一下就能得出95031班学生每门课的平均成绩:
SELECT c_no, AVG(degree) FROM score WHERE s_no IN (SELECT no FROM student WHERE class = '95031') GROUP BY c_no; +-------+-------------+ | c_no | AVG(degree) | +-------+-------------+ | 3-105 | 82.0000 | | 3-245 | 71.5000 | | 6-166 | 80.0000 | +-------+-------------+
子查询 - 1
查询在 3-105
课程中,所有成绩高于 109
号同学的记录。
先用子查询查找出109同学在3-105中的成绩
select * from score where c_no ='3-105' and s_no='109'
然后再以课程3-105为条件,查找成绩大76的记录
select * from score where c_no = '3-105' and degree> (select degree from score where c_no = '3-105' and s_no='109');
子查询 - 2
查询所有成绩高于 109
号同学的 3-105
课程成绩记录。
-- 不限制课程号,只要成绩大于109号同学的3-105课程成绩就可以。 SELECT * FROM score WHERE degree > (SELECT degree FROM score WHERE s_no = '109' AND c_no = '3-105');
YEAR 函数与带 IN 关键字查询
查询所有和 101
、108
号学生同年出生的 no
、name
、birthday
列。
select no, name, birthday from student where year(birthday) in ( select year(birthday) from student where no in (101,108));
多层嵌套子查询
查询 '张旭'
教师任课的学生成绩表。
用的三张表teacher、course、score,首先找到教师编号:
SELECT NO FROM teacher WHERE NAME = '张旭'
通过
sourse
表找到该教师课程号:
select no from course where t_no = (SELECT NO FROM teacher WHERE NAME = '张旭')
通过筛选出的课程号查询成绩表:
select * from score where c_no = ( select no from course where t_no = ( select no from teacher where name = '张旭'));
多表查询
查询某选修课程多于5个同学的教师姓名。
首先在teacher
表中,根据no
字段来判断该教师的同一门课程是否有至少5名学员选修:
-- 查询 teacher 表 SELECT no, name FROM teacher; +-----+--------+ | no | name | +-----+--------+ | 804 | 李诚 | | 825 | 王萍 | | 831 | 刘冰 | | 856 | 张旭 | +-----+--------+ SELECT name FROM teacher WHERE no IN ( -- 在这里找到对应的条件 );
查看和教师编号有有关的表的信息:
SELECT * FROM course; -- t_no: 教师编号 +-------+-----------------+------+ | no | name | t_no | +-------+-----------------+------+ | 3-105 | 计算机导论 | 825 | | 3-245 | 操作系统 | 804 | | 6-166 | 数字电路 | 856 | | 9-888 | 高等数学 | 831 | +-------+-----------------+------+
我们已经找到和教师编号有关的字段就在
course
表中,但是还无法知道哪门课程至少有5名学生选修,所以还需要根据score
表来查询:
-- 在此之前向 score 插入一些数据,以便丰富查询条件。 INSERT INTO score VALUES ('101', '3-105', '90'); INSERT INTO score VALUES ('102', '3-105', '91'); INSERT INTO score VALUES ('104', '3-105', '89'); -- 查询 score 表 SELECT * FROM score; +------+-------+--------+ | s_no | c_no | degree | +------+-------+--------+ | 101 | 3-105 | 90 | | 102 | 3-105 | 91 | | 103 | 3-105 | 92 | | 103 | 3-245 | 86 | | 103 | 6-166 | 85 | | 104 | 3-105 | 89 | | 105 | 3-105 | 88 | | 105 | 3-245 | 75 | | 105 | 6-166 | 79 | | 109 | 3-105 | 76 | | 109 | 3-245 | 68 | | 109 | 6-166 | 81 | +------+-------+--------+ -- 在 score 表中将 c_no 作为分组,并且限制 c_no 持有至少 5 条数据。 SELECT c_no FROM score GROUP BY c_no HAVING COUNT(*) > 5; +-------+ | c_no | +-------+ | 3-105 | +-------+
根据筛选出来的课程号,找出在某课程中,拥有至少5名学员的教师编号:
SELECT t_no FROM course WHERE no IN ( SELECT c_no FROM score GROUP BY c_no HAVING COUNT(*) > 5 ); +------+ | t_no | +------+ | 825 | +------+ 在teacher表中,根据筛选出来的教师编号找到教师姓名:SELECT name FROM teacher WHERE no IN ( -- 最终条件 SELECT t_no FROM course WHERE no IN ( SELECT c_no FROM score GROUP BY c_no HAVING COUNT(*) > 5 ) );
子查询 - 3
查询 “计算机系” 课程的成绩表。
思路是,先找出teacher表中所有计算机系
课程的编号,根据这个编号查询course表中的课程编号,再用课程编号查找score表
-- 通过 teacher 表查询所有 `计算机系` 的教师编号 SELECT no, name, department FROM teacher WHERE department = '计算机系' +-----+--------+--------------+ | no | name | department | +-----+--------+--------------+ | 804 | 李诚 | 计算机系 | | 825 | 王萍 | 计算机系 | +-----+--------+--------------+ -- 通过 course 表查询该教师的课程编号 SELECT no FROM course WHERE t_no IN ( SELECT no FROM teacher WHERE department = '计算机系' ); +-------+ | no | +-------+ | 3-245 | | 3-105 | +-------+ -- 根据筛选出来的课程号查询成绩表 SELECT * FROM score WHERE c_no IN ( SELECT no FROM course WHERE t_no IN ( SELECT no FROM teacher WHERE department = '计算机系' ) ); +------+-------+--------+ | s_no | c_no | degree | +------+-------+--------+ | 103 | 3-245 | 86 | | 105 | 3-245 | 75 | | 109 | 3-245 | 68 | | 101 | 3-105 | 90 | | 102 | 3-105 | 91 | | 103 | 3-105 | 92 | | 104 | 3-105 | 89 | | 105 | 3-105 | 88 | | 109 | 3-105 | 76 | +------+-------+--------+
UNION 和 NOT IN 的使用
查询计算机系
与电子工程系
中的不同职称的教师。
+-----+------+-----+------------+------------+------------+ | no | name | sex | birthday | profession | department | +-----+------+-----+------------+------------+------------+ | 804 | 李诚 | 男 | 1958-12-02 | 副教授 | 计算机系 | | 825 | 王萍 | 女 | 1972-05-05 | 助教 | 计算机系 | | 831 | 刘冰 | 女 | 1977-08-14 | 助教 | 电子工程系 | | 856 | 张旭 | 男 | 1969-03-12 | 讲师 | 电子工程系 | +-----+------+-----+------------+------------+------------+ -- NOT: 代表逻辑非 SELECT * FROM teacher WHERE department = '计算机系' AND profession NOT IN ( SELECT profession FROM teacher WHERE department = '电子工程系' ) -- 合并两个集 UNION SELECT * FROM teacher WHERE department = '电子工程系' AND profession NOT IN ( SELECT profession FROM teacher WHERE department = '计算机系' );
ANY 表示至少一个 - DESC ( 降序 )
查询课程 3-105
且成绩 至少 高于 3-245
的 score
表。
SELECT * FROM score WHERE c_no = '3-105'; +------+-------+--------+ | s_no | c_no | degree | +------+-------+--------+ | 101 | 3-105 | 90 | | 102 | 3-105 | 91 | | 103 | 3-105 | 92 | | 104 | 3-105 | 89 | | 105 | 3-105 | 88 | | 109 | 3-105 | 76 | +------+-------+--------+ SELECT * FROM score WHERE c_no = '3-245'; +------+-------+--------+ | s_no | c_no | degree | +------+-------+--------+ | 103 | 3-245 | 86 | | 105 | 3-245 | 75 | | 109 | 3-245 | 68 | +------+-------+--------+ -- ANY: 符合SQL语句中的任意条件。 -- 也就是说,在 3-105 成绩中,只要有一个大于从 3-245 筛选出来的任意行就符合条件, -- 最后根据降序查询结果。 SELECT * FROM score WHERE c_no = '3-105' AND degree > ANY( SELECT degree FROM score WHERE c_no = '3-245' ) ORDER BY degree DESC; +------+-------+--------+ | s_no | c_no | degree | +------+-------+--------+ | 103 | 3-105 | 92 | | 102 | 3-105 | 91 | | 101 | 3-105 | 90 | | 104 | 3-105 | 89 | | 105 | 3-105 | 88 | | 109 | 3-105 | 76 | +------+-------+--------+