我有一个名为
user
的表格,如下所示:user_id name gender age
--------------------------------
1 AAA Male 45
2 BBB Female 22
3 CCC Male 47
.................................
..............................
我想获取用户总数、男性用户总数以及男女用户的百分比。
select count(*) as total , SUM(IF(v.gender = 'Male',1,0)) as totalMale ,
SUM(IF(v.gender = 'Female',1,0)) as totalFemale , totalMale/total *100 ,
totalFeMale/total *100 from user;
当使用别名计算男性和女性百分比时,此查询无法正常工作。我收到了以下错误:unknown columns..........
select count(*) as total , SUM(IF(v.gender = 'Male',1,0)) as totalMale ,
SUM(IF(v.gender = 'Female',1,0)) as totalFemale , SUM(IF(v.gender = 'Male',1,0))/count(*) *100 ,SUM(IF(v.gender = 'Female',1,0))/count(*) *100 from user;
但是这个是有效的。
但是我使用了SUM(IF(v.gender = 'Female',1,0))
两次。我觉得这会降低性能。
在我的情况下,我不能使用别名吗?
提前感谢...
COND1 and COND2 and COND3
将始终返回true或false(对于非空操作数)。 - Mosty Mostacho