班级与性别对应表
classId
sex要求写出查询男女相等的班级的SQLclassId sex
1  男
1  女
1  男
1  女
2  男
2  女
2  男
3  男
3  女
3  女
4  男
4  女
帮帮小弟吧

解决方案 »

  1.   

    表结构简单啊
    就是classId Int(10),
    sex varchar(50),
      

  2.   

    SELECT a.classId,b.* FROM user a,user b where count(a.sex)=count(b.sex) and a.sex='男' and b.sex='女' and a.classId=b.classId
    不知道哪有有错,请老大们帮忙说说!
      

  3.   

    select a.classId
    from (select classId,count(*) as cnt from 表结构 where sex='男' group by classId) a
    ,  (select classId,count(*) as cnt from 表结构 where sex='女' group by classId)  b
    where a.classId=b.classId and a.cnt=b.cnt
      

  4.   


    SELECT classId  FROM TTH
    GROUP BY classId HAVING SUM(IF(sex='男',1,0))=SUM(IF(sex='女',1,0))