MySQL查询组由多个条件依赖

时间:2017-12-18 17:50:11

标签: mysql

我有以下格式的选民数据的MySQL表格(voters):

id | name | gender | const 

我需要输出以下内容:

const | no. of female voters | no. of male voters | total no. of voters

order by没有。女选民。

我只能提出这个问题:

select const,count(*) from voters where gender='f' group by const order by count(*) desc

我如何得到其他两项罪名? const表示选区,性别可以是'm'或'f'

1 个答案:

答案 0 :(得分:0)

在Mysql中你可以这样做

select const,
count(*),
sum(gender='f') female_voters,
sum(gender='m') male_voters
from voters 
group by const 
order by count(*) desc