自连接
子查询: 在其他SQL中嵌入查询语句
单行子查询:
1.查询和"不想毕业"同班的同学有哪些
select name from student where classes_id = (select classes_id from student where name = '不想毕业');
多行子查询
查询语文或者英文课程对应的成绩
select score from score where course_id in (select id from course where name = '语文' or name = '英文');
先执行子查询,把子查询的结果保存到内存中,再进行内查询,再结合刚才的子查询结果来筛选最终结果,
借助exist完成
select * from score where exists(select score.course_id from course where (name = '语文' or name = '英文') and score.course_id = course.id);
先执行主查询,再去除法子查询.对两个结果进行综合的筛选,
合并查询
相当于把多个查询的结果集合合成并成一个集合(需要保证结果集之间的字段类型和数目都得一致)
union
查询id < 3或者名字为英文的课程
select * from course where id < 3 union select * from course where name = '英文';
这是把两个查询后的结果合并到了一起
如果两个查询结果存在相同的记录,只会保留一个
如果不想去重,可以使用union all