--没结果
select stu.sno,stu.sname from student stu
where (select count(*) from sc sc inner join student stu on sc.sno=stu.sno) =5--有结果
select stu.sno, stu.sname from student stu
where (select count(*) from sc where sno=stu.sno) = 5
是什么原因?另外有没有不用子查询的办法呢?
select stu.sno,stu.sname from student stu
where (select count(*) from sc sc inner join student stu on sc.sno=stu.sno) =5--有结果
--这个可以先这样理解一下,上面的也一样的理解,应该明白了
select stu.sno, stu.sname,(select count(*) from sc where sno=stu.sno)
from student stu
where
(select count(*) from sc where sno=stu.sno) = 5
select stu.sno, stu.sname from student stu
where sno IN
(select sno from sc GROUP BY sno HAVING COUNT(1)= 5)
这是统计有选课的人数,如果选课总次数不是5,就相当于查询条件否定,结果当然为0第二个是采用关联查询,才能查询选修5门个的人
where (select count(*) from sc sc inner join student stu on sc.sno=stu.sno) =5
====>
select stu.sno,stu.sname from student stu
where M =5
--M 就是一个定值