本节课内容:
1:复合连接条件查询
2:子查询
1:知识点一:复合连接条件查询
on连接的表达式有多个,称为复合连接条件。
两个表以上的连接称为多个连接》
问:查询成绩在75分以上的学生的学号、姓名,选修课任课老师的教师编号、姓名?
select A.stu_num,A.stu_name,B.score,C.t_num,C.name
from student A join regedit B
on A.stu_num = B.stu_num and B.score>75
join teacher C on B.teacher_num = C.t_num;
2:知识点二:子查询
在where子句或having子句中查询称之为嵌套查询。--称为子查询或内层查询
三种类型子查询:
1)通过in、not in、any、all引入
2)通过比较运算符引入
带in的子查询案列:-子查询的结果是一个集合
案列1:使用系部表 和 班级表 ,查询计算机系和经济管理系的班级信息。
select * from class
where dept_num in
( select dept_num from dept
where dept_name = '计算机系'
or dept_name = '经济管理系')
案例2:查询选修了“数据库应用”的学生学号和姓名?
方法一:
组合:
select stu_num,stu_name from student where
stu_num in
(select stu_num from regedit where
course_num in
(select c_num from course where c_name='数据库应用')
)
方法二:
select student.stu_num,student.stu_name from student join regedit on student.stu_num = regedit.stu_num
join course on regedit.course_num = course.c_num and course.c_name='数据库应用'
注意:子查询的select子句总是用括号括起来
not in 与in相反,对比学习;
带比较运算符的子查询
比较运算符:<,>,=...
案列:列出选修了“0001”号课程,成绩高于该课程
平均分的学生信息:
select stu_num from regedit
where score >
(select avg(score) from regedit where
scourse_num = '0001') and scourse_num = '0001'
any的使用
查询比“060101001”班中所有学生年龄小的其他班的学生学号与姓名?
select 学号,姓名
from 学生
where 出生日期>all(select 出生日期 from 学生 where 班级代码=‘060101001’)
and 班级代码<>'060101001';
1:select ... from 表 where 条件 group by ...having ...order by..desc[asc]
2: select ... from 表1 join 表2 on ... join 表3 on...
3:in any < >...