联合查询 union
union all:使用union all 的效果相当于将多个查询的结果按顺序一起输出
union :使用union 的效果相当于在 union all的基础上,会筛选掉重复的,且输出第一次出现的
使用联合查询时需要注意,这些联合的查询需要保持查询的列的数量相同,字段类型相同;
嵌套查询(子查询)
按子查询结果分类:标量子查询(单个具体值),列子查询(一列,多个值),行子查询(一行多列),表子查询(多行多列)
标量子查询
标量子查询:返回的结果是单个值(数字,字符串等具体的值)
常用操作符:= <> > >= < <=
列子查询
列子查询:返回的结果是一列多个值或者(多行,但一行一个值)
常用操作符:in (满足条件中的一个就行) , not in (不在条件范围内), any ( 满足条件中的一个就行) , some 满足条件中的一个就行) , all (都得满足)
查询同时参加计算机和英语考试的学生的信息
select * from student where Num in(select Stu_id from scroe where C_name='计算机' intersect select Stu_id from scroe whereC_name ='英语')
/*第一步,查询同时参加计算机和英语考试的学生的学号,结果为901,904,905*/
select Stu_id from scroe where C_name='计算机' intersect select Stu_id from scroe whereC_name ='英语'
/*第二步,根据学号查询学生信息*/
select * from student where Num in(901,904,905)
行子查询
列子查询:返回的结果是一行或者多列
常用操作符:= , <> ,in , not in ,
表子查询
查询计算机高于90的成绩信息,以及学生信息
select * from scroe where C_name='计算机' and grade>=90
select sc.*,st.*from (select * from scroe where C_name='计算机' and grade>=90) as sc left join student as st on sc.Stu_id=st.Num