SQL查询有关问题,对来说应该较简单

SQL查询问题,对各位高手来说应该较简单!

班级与性别对应表

classId

sex

要求写出查询男女相等的班级的SQL

classId sex

1 男

1 女

1 男

1 女

2 男

2 女

2 男

3 男

3 女

3 女

4 男

4 女

帮帮小弟吧



SQL code

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





SELECT classId FROM TTH
GROUP BY classId HAVING SUM(IF(sex='男',1,0))=SUM(IF(sex='女',1,0))
SQL查询有关问题,对来说应该较简单

相关文章:

你感兴趣的文章:

标签云: