【发布时间】:2016-09-26 03:32:41
【问题描述】:
我有一个收集选票的基本投票系统。制表应以总赞成票除以总票数来确定是否达到 2/3 多数。 目前,我可以使用此查询返回数据
select sum(case when vote is not null and recruit_id=49631 then 1 else 0 end)
as total_votes,
sum(case when vote=1 and recruit_id=49631 then 1 else 0 end) as total_yes from votes;
返回
+-------------+-----------+
| total_votes | total_yes |
+-------------+-----------+
| 3 | 2 |
+-------------+-----------+
我想做的是这样的
+-------------+-----------+-----------+
| total_votes | total_yes | YESPercent|
+-------------+-----------+-----------+
| 3 | 2 | 66.6 |
+-------------+-----------+-----------+
我尝试过使用类似的东西:
select sum(case when vote is not null and recruit_id=49631 then 1 else 0 end) as total_votes,
sum(case when vote=1 and recruit_id=49631 then 1 else 0 end) as total_yes,
sum(total_votes,total_yes,(total_yes/total_votes)*100) as YESPercent from votes;
它无法识别最后部分的 total_yes 或 total_votes。有任何提示或指向良好指导的链接吗?
【问题讨论】:
标签: mysql sql select percentage