• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

数据库not exists 嵌套查询

mysql 搞代码 4年前 (2022-01-09) 92次浏览 已收录 0个评论

查询选修了全部课程的学生姓名 SQL语句: select Sname———称它为第一层查询 from student where not exists( select *——————称它为第二层查询 from course where not exists( select *—————–称它为第三层查询 from sc where Sn

查询选修了全部课程的学生姓名

SQL语句:

select Sname ———称它为第一层查询
from student
where not exists(
select * ——————称它为第二层查询
from course
where not exists(
select * —————–称它为第三层查询
from sc
where Sno = student.Sno AND Cno = course.Cno));

总体理解:

最内部的 select * from sc where cno = course.cno and sno=student.sno是查询出所有已经选择过课程的学生及相应课程,

select * from course where not exists 则是所有没有被选择的课程,

在这个基础上的 select sname from student where not exists 则是选取所有没有未选择课程的学生,即选择了所有课程的学员名称。

具体查询过程(猜测):

第一层查询from student(从student表中取出一个元祖)—->where not exists(判断where子句)—->

第二层查询from course(从courset表中取出一个元祖)—->where not exists(判断where子句)—->

第三层查询用from student中取出的一个元组和fro

本文来源gaodai.ma#com搞##代!^码@网3

m course 中取出的一个元组在sc表中查询,满足Sno = student.Sno AND Cno = course.Cno的,第二层查询的where子句返回false,不满足的返回true。从第二层查询中from course(从courset表中取出第二个元祖)—->where not exists(判断where子句),返回,·········直到course表中元组被取完。如果全部都是false,则student表中的Sname被放入结果集,否则不放入。

循环,取student表的第二个元组··············

直到student表中的元组取完,结束。

参考资料:http://wenku.baidu.com/view/1206bed97f1922791688e81b.html


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:数据库not exists 嵌套查询

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址