sql exists的问题

来源:百度知道 编辑:UC知道 时间:2024/06/12 17:59:00
学生表s(s#,sname,sex,age)
课程表c(c#,cname)
选课表sc(s#,c#,grad)
select c#,cname from c
where not exists(select* from s where not exists
(select* from sc where s#=s.s# and c#=c.c#));
其中not exists怎么理解,整个查询怎么看 ,谢谢了
其中嵌套的 SELECT * ,*代表什么?

not exists表示在你所说的条件中不存在的内容

1、select * from sc where s#=s.s# and c#=c.c#意思是当所说的这两个编号相等时,查询出选课的记录

2、select * from s where not exists (select * from sc where s#=s.s# and c#=c.c#)意思是:从学生表中查询出来没有括号内选课的学生的记录

3、select c#,cname from c where not exists(select * from s where not exists (select * from sc where s#=s.s# and c#=c.c#)); 意思是查询出2结果中没有的内容。

select * 表示你要查询这个表中的所有字段。

他们解释的都挺好的,可能只是你的只是有限,没有看明白而已。努力学习吧!

*这里没有太多意义,表示存在记录。
exists的话先对外部做select查询,然后外部select结果再去匹配exists中的条件,符合条件的就是需要的记录。
所以说这里的*可以用任何标记代替,1、0,随便什么

所有字段
不存在