select count(sid) from (select sid from student where bid in('100','101','102') group by sid having count(1)=3)a

解决方案 »

  1.   

    谢谢LBYYBL,不过还有个问题没有解决,比如上面的数据,我要求统计同时选择101、102的数量是2而不是4。
      

  2.   

    select count(*)
    from
    (select distinct sid from Table where bid='100') a join
    (select distinct sid from Table where bid='101') b on a.sid=b.sid
    (select distinct sid from Table where bid='102') c on a.sid=c.sid
      

  3.   

    select count(distinct sid) from student 
    where sid in(select sid from student group by sid having count(bid)=3)
      

  4.   

    select count(sid) from (select sid from student where bid in('101','102') group by sid having count(1)=2) a left join
    (select sid from student where bid not in('101','102')) b on a.sid=b.sid 
    where b.sid is null
      

  5.   

    select count(distinct sid) 
    from student 
    where sid in(select sid 
                 from student 
                 where bid in('100','101','102')
        group by sid
        having count(bid)=3)