带EXISTS谓词的查询
- 存在谓词,不返回数据,只返回逻辑真和假
语法:
select 列名列表
from 表名1
where [not] exists (select * from 表名2 [where子句])
执行步骤
1,父查询传送列值给子查询
2,子查询获取父查询传送的列值1
3,子查询返回查询值给父查询
4,父查询传送下一列的值给子查询(重复1-3步)
例子:
- 使用exists谓词查询所有成绩都在85分以上的同学的学号和姓名
select StudID,StuName
from Student join SC on Student.StuID = SC.StuID -- 连接查询是为了防止有人没选课(没成绩),还出现在结果集中
where not exists(select * from SC where StuID=Student.StuID and Score < 85)
select * 和and 后面的字段就是exists关键字的规律!