--2020-3-27
--exists谓词 相关子查询(内层子查询一定是*)
--当返回结果为真的时候才会列出结果
--查询所有选修了1号课程的学生姓名
--select Sname
--from student
--where sno in
--(
--select sno
--from sc
--where cno = '1'
--);
--select sname
--from student
--where exists/*检测部分表,只要存在就截止*/
--( /* 从父查询中拿出每一条数据来和内层比较*/
-- select * /* 内层并不关心查到了什么,只关心有没有,所以用的是'*' */
-- from sc
-- where student.sno = sc.sno and cno='1');/*相关子查询,外层给内层提供条件*/
--select sname
--from student
--where not exists/*遍历整个表*/
--( /* 从父查询中拿出每一条数据来和内层比较*/
-- select * /* 内层并不关心查到了什么,只关心有没有,所以用的是'*' */
-- from sc
-- where student.sno = sc.sno and cno='1');/*相关子查询,外层给内层提供条件*/
--not exists 谓词(内层子查询一定是*)
--结果为空的时候才是真
--用exists 和not exists 实现全称量词(SQL本身并没有全程量词)
--查询选修了全部课程的学生姓名(双重否定表肯定)
--select sname/*外层提供学号,中间层提供课程号*/
--from student/*最外层的查询是不存在某个课程所查的人没有选择,即选了所有的课*/
--where not exists
-- (select */*这部分查询查的是不在已经选的课程的信息*/
-- from course
-- where not exists
-- (select * /*这部分查询是已经 选了的课 的信息*/
-- from sc
-- where student.sno=sc.sno and
-- course.cno = sc.cno
-- ));
/*先找到选了的课a,再根据已经选了的课找出没有选的课b,
如果不存在这样的课b,即选了所有的课*/
--用exists/not exists实现逻辑蕴含(难点)
--查询至少选修了201215122选修的全部课程的学生的学号
--select sno
--from sc scx
--where not exists
--(select *
-- from sc scz
-- where sno = '201215122' and not exists
-- (select *
-- from sc scz
-- where scx.sno=scz.sno and scx.cno=scz.cno
-- )
-- );/*思路同上例*/
--不存在这样的课程y,学生201215122选修了y,而学生sno没有选。
--集合操作
--并操作union 可以用and替代
--select *
--from student
--where sdept='CS' /* where sdept='CS' or sage<=19*/
--union
--select *
--from student
--where sage<=19
--交操作intersect
2020-3-27 exists/not exists
猜你喜欢
转载自blog.csdn.net/u013140841/article/details/105380298
今日推荐
周排行