50个查询系列-第10个查询:查询没有学全所有课的同学的学号、姓名;

我们来说一下思路:

1:先找所开的所有的课程

2.在遍历每一个学,看在学生的课表里面是不是都有这些课,有的话说明全选了,没有的话就是没有全选。用NOT IN

这里的not in 和IN 有区别,我刚开始用 notexists in不行,后来用了exists not in 就好了。查出来的结果都不一样。

select  tblstudent.StuName,tblstudent.StuId from tblstudent 

where 

 EXISTS
(
select * from 
(SELECT  tblcourse.CourseId t1 FROM  tblcourse)t-- t1是全部的课程
where 
t.t1
not IN-- 全部的课程在学生上的课程里面都不存在那就说明学生没有选满全部的课,因为如果学生选了全部的课程,在学生的课表里就能找到所有的课
(select tblscore.CourseId from tblscore where tblstudent.StuId=tblscore.StuId))

答案的做法是:

Select StuId,StuName From tblStudent st
  Where (Select Count(*) From tblScore sc Where st.StuId=sc.StuId)<
   (Select Count(*) From tblCourse)
原文地址:https://www.cnblogs.com/shenxiaoquan/p/6121656.html