我有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))
了 2 次吗?我认为它会降低性能。
我不能在我的情况下使用别名吗?
提前致谢...