mysql中not exists
表结构:
Student(Sno, Sname, Ssex, Sage, Sdept)
Course(Cno, Cname, Cpno, Ccredit)
Sc(Sno, Cno, Grade)
题目
查询选修了全部课程的学生姓名
即:没有一门课程是没有选修的
select Sname
from Student
where not exists(
select *
from Course
where not exists(
select *
from sc
where Student.Sno = sno and Cno = Course.Cno
)
)
执行顺序
首先取出Student的第一行,再取出Course的第一行,在最内层子查询判断表sc是否存在满足条件的行,若存在,(select * from sc where Student.Sno = sno and Cno = Course.Cno)返回FALSE,select * from Course where not exists返回TRUE,最后返回Student的第一行的Sanme,反之相反。
下次再选择Course的第二行,直到遍历Course表。之后选择Student的第二行
循环执行上述操作。