SQL not exists双重否定
学生表 S(sid,name,sex,age)
课程表 C(cid,name)
学生选课关联表 SC(sid,cid,score)
查询选修全部课程的学生姓名:
select S.name from S where not exists(
select * from C where not exists(
select * from SC where sid=S.sid and cid=C.cid
)
)
解释:
最里层的子查询用于查找某个学生选修的所有课程;
中间层的子查询(结合not exists)用于查找该学生没有选修的课程;
最外层的查询(结合not exists)用于查找不存在“有几门课程没有选修”情况的学生,即选修了全部课程
原文链接:http://hi.baidu.com/liutingrex/blog/item/695a127bfeaddff20bd18718.html