我创建了一个像这样的查询
"select count(if(gender="male",1,null)) as malecount,
count(if(gender="female",1,null)) as femalecount from biodata"
这是工作,我想总结这样的计数:
"select count(if(gender="male",1,null)) as malecount,
count(if(gender="female",1,null)) as femalecount, male+female as sumgender
from biodata"
当我在if语句中提出许多条件时,查询将有所帮助
请帮我解决这个案子......
答案 0 :(得分:0)
要么
select
count(if(gender="male",1,null)) as malecount,
count(if(gender="female",1,null)) as femalecount,
count(if(gender in ("female","male",1,0)) as sumgender
from biodata
或者如果您确定所有行都有男性或女性作为性别,请使用简单的计数(1)
select
count(if(gender="male",1,null)) as malecount,
count(if(gender="female",1,null)) as femalecount,
count(1) as sumgender
from biodata
或做一个子查询
select
malecount,
femalecount,
malecount + femalecount as sumgender
from (
select
count(if(gender="male",1,null)) as malecount,
count(if(gender="female",1,null)) as femalecount
from biodata
) sub
但您无法使用聚合别名在同一个选择中进行计算。
答案 1 :(得分:0)
用这种方式解决问题
select malecount+femalecount as sumcount from (select
count(if(gender="male",1,null)) as malecount,
count(if(gender="female",1,null)) as femalecount from biodata) as
query