我有以下格式的选民数据的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'

最佳答案

在Mysql中,您可以执行此操作

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

07-26 02:06