--没结果
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
是什么原因?另外有没有不用子查询的办法呢?

解决方案 »

  1.   

    --没结果
    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
      

  2.   


    select stu.sno, stu.sname from student stu
    where sno IN 
    (select sno from sc  GROUP BY sno HAVING COUNT(1)= 5)
      

  3.   

    select count(*) from sc sc inner join student stu on sc.sno=stu.sno
    这是统计有选课的人数,如果选课总次数不是5,就相当于查询条件否定,结果当然为0第二个是采用关联查询,才能查询选修5门个的人
      

  4.   

    select count(*) from sc sc inner join student stu on sc.sno=stu.sno这是一个确定的值呀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 M =5
    --M 就是一个定值