如何从表列中查找百分比值

时间:2016-01-10 23:30:54

标签: php mysql percentage

我正在尝试在表格中找到具有相同ID的每个数值的百分比值。例如,如果我的表包含以下内容:

id  answer          points
1   answer 1        3
1   answer 2        1
1   answer 3        10
1   answer 4        5
1   answer 5        6
1   answer 6        10
1   answer 7        10
1   answer 8        2

如果最大点数为80,如何以百分比形式显示数据库中的当前结果(点)?

3 个答案:

答案 0 :(得分:2)

求点()并将它们除以80:

select id, sum(points), sum(points)/80 as percentage
from table
group by id

答案 1 :(得分:1)

如果我理解正确:

select   100 * (points / 80) as Percentage
from     YourTable

您甚至可以考虑计算总分数:

select   100 * (points / (select sum(points) from YourTable)) as Percentage
from     YourTable

这样,您只需添加表格的答案,计算就会相应调整。

答案 2 :(得分:0)

您可以将GROUP BY用于此目的,例如

select maxpoint * 0.1 as maxpoint_percent
from (
select id, sum(points) as maxpoint
from table1
group by id ) xx
where maxpoint >= 80;