您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

数据库not exists 嵌套查询

2025/8/28 6:29:23发布57次查看
查询选修了全部课程的学生姓名 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中取出的一个元组和from 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
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product