子查询注意事项:
1.子查询可以代替任意多表联查
多表联查不能代替子查询
2.当子查询的结果作为条件时如果查询出多个结果则不能使用赋值运算符
只能使用in,not in是取反的意思
3.子查询只能写在()里
4.子查询不能使用在聚合函数中
5.beetween操作符不能与子查询一起使用
子查询的使用
1.查找本张表和另外一张表的列
select 本表列名,(select 别表列名 from 别表 where 本表公共列名=别表公共列名) from 本表名
2.子查询的结果作为一个条件
1.子查询作为条件时只有一个结果
select from 本表名 where 列名=(select 别表列名 from 别表名 where 条件)
2.子查询作为条件时有多个结果,要将=改为 in
select from 本表名 where 列名 in (select 别表列名 from 别表名 where 条件)
3.exists使用说明
1.exists
—()里的子查询语句的结果有数据则exists返回的结果为true,否则返回false
select * from 本表名 where exists(select 别表列名 from 别表 where 本表公共列名=别表公共列名)
2.not exists
将exists的结果取反,如果exists ()里的子查询有数据返回,
exists返回true,not exists返回false
