Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP/Mysql统计轮询的百分比_Php_Mysql_Algorithm - Fatal编程技术网

使用PHP/Mysql统计轮询的百分比

使用PHP/Mysql统计轮询的百分比,php,mysql,algorithm,Php,Mysql,Algorithm,我正在使用投票系统,我想计算选择此变量的时间百分比。 例如,我有4个可用的变体 Poll name | Vote number Java | 250 Python | 200 C# | 90 PHP | 1 我的民意测验中有3列dbId、Tex、Creator\u Id 在Poll_variantsMyDB中有4列问题 Id | Poll_id | Variant | Vote_num 1 | 1

我正在使用投票系统,我想计算选择此变量的时间百分比。
例如,我有4个可用的变体

Poll name | Vote number  
  Java    |     250
  Python  |     200
  C#      |      90
  PHP     |      1
我的
民意测验中有3列
db
Id、Tex、Creator\u Id

Poll_variants
MyDB中有4列问题

Id | Poll_id | Variant | Vote_num
1  |    1    |  Java   | 250 
1  |    1    |  Python | 200 
1  |    1    |  C#     | 90 
1  |    1    |  PHP    | 1 

查找子查询中每个poll_id的投票总数,并将其与主表联接以查找每个变量的百分比

select t1.*, 100 * t1.votes/t2.total_votes percentage
from Poll_variants t1
inner join (
  select poll_id, sum(votes) total_votes
  from Poll_variants
  group by poll_id
) t2 on t1.poll_id = t2.poll_id;

您可以使用以下SQL

SELECT *,Votes*100/(SELECT sum(Votes) from Poll_variants)total FROM Poll_variants WHERE 1

@古尔夫,我补充了你的精神wished@user7302801你能告诉我们你想要的结果是什么吗?@GurV这次我能满足你的精神吗?@reza
Java-46%
Python-36%
C.-16%
php-0.1%
您能解释一下t1和t2吗?我的mysql知识边界停在
Join
functiont1和t2分别是您的_表和子查询的别名。我们使用它们,这样我们就不必像
你的表那样写。一些列
到处都是。t1是我的民意测验表还是问题表?两者都不是-它是实际表名的另一个名称。我更新了我的答案,将
your_table
更改为
questions
我尝试了这个代码,不幸的是它返回了一个新的列,其中
NULL
是什么
count*100
?这个代码返回了一个新的列
total
NULL
值在尝试之前你在我的代码中更改了表名吗??。我已经用您的表名更新了我的查询。它仍然返回空值吗??