【notexists】...select*fromcoursewherenotexists(select*fromscwheresn】

发布时间:2021-03-25 14:34:48

SQL查询选修了全部课程的学生姓名查询选修了全部课程的学生姓名:student 是学生表 course 是选课表 sc 是课程表select snamefrom studentwhere not exists(select *from coursewhere not exists (select *from scwhere sno =student.snoand cno=course.cno)select *from coursewhere not exists (select *from scwhere sno =student.snoand cno=course.cno)这一段计算为什么就得出 某学生存在没有选的课程?

网友回答

【答案】 第一问:两个NOT EXISTS表示双重否定:没有一个选了课的学生没有选course表里的课程
  select sname
  from student
  where not exists /*没有一个学生满足以下条件*/
  (select * from course
  where not exists /*什么条件呢?没有选过Course表里的课*/
  (select * from sc
  where sno =student.sno /*这里两个=分别指对应的关系,表示选过课并且是Course里and cno=course.cno) 的课,只不过用not exists否定掉了*/
  第二问:其实和NOT IN 是一个意思 exists只返回true 或false 这里not exists里的内容 其实就是指学生选过的课程,再用NOT EXISTS否定了,就变成了没有选的
以上问题属网友观点,不代表本站立场,仅供参考!