exists、not exists内部子句与外部子句无联系时
select * from dept where exists(select * from dept where 1=1);
- 首先,取外部dept 中一条数据
- 然后,取exists内部dept中一条数据
- 根据where 条件1=1判断条件是否成立,成立返回true,不成立返回false
- 输出外部select * from dept匹配上的数据
注:读者也可将exists括号中子句看作(1=1)
select * from dept where not exists(select * from dept where 1=1);
- 首先,取外部dept 中一条数据
- 然后,取exists内部dept中一条数据
- 根据where 条件1=1判断条件是否成立,成立返回false,不成立返回true
- 输出外部select * from dept匹配上的数据
注:读者也可将 not exists括号中子句看作(1=1)
exists、not exists内部子句与外部子句有联系时
select sname from student s where not exists(select sno from sc where s.sno=sc.sno);
- 首先取外部student中一条数据
- 其次取内部sc表中一条数据
- 根据where条件s.sno!=sc.sno判断其是否匹配,匹配返回false,不匹配返回true(循环过程只要sc表中有一条与其匹配就能输出)
- 输出外部select sname from student匹配的字段
动画解释
select sname from student s where not exists(select sno from sc where s.sno=sc.sno);
