六、子查询——多行子查询
1.什么是多行子查询
子查询结果为一列值,主查询与子查询之间使用in、not in、any、all等连接。
2.多行子查询实例
(1)示例1
查询有成绩的同学的基本信息
<1>从成绩表中查询所有学生成绩不为空的学号
select student_id from student_score where score is not null;
<2>从学生表中查询所有基本信息【总的SQL语句】
select * from student
where student_id in (select student_id from student_score where score is not null);
(2)示例2
查询没有成绩的同学的基本信息
<1>从成绩表中查询所有学生成绩不为空的学号
select student_id from student_score where score is not null;
<2>从学生表中查询所有基本信息【总的SQL语句】
select * from student
where student_id not in (select student_id from student_score where score is not null);
3.注意点
尽量减少使用not in,效率不高。
解锁课程,学习更多mysql知识:
https://study.163.com/course/courseMain.htm?courseId=1209590958&share=2&shareId=400000000537035