Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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 按百分比列出_Php_Mysql_Join_Percentage - Fatal编程技术网

Php 按百分比列出

Php 按百分比列出,php,mysql,join,percentage,Php,Mysql,Join,Percentage,我正在努力提高考试答案的百分比 我有3个表的mysql数据库 表测试 id | name ---+-------- 1 | test 1 2 | test 2 表问题及测试问题 id | name | test_id ---+------------+-------- 1 | question 1 | 1 2 | question 2 | 1 3 | question 3 | 1 4 | question 4 | 1 5 | question 1 | 2 6 | q

我正在努力提高考试答案的百分比

我有3个表的mysql数据库

测试

id | name
---+--------
1  | test 1
2  | test 2
问题
及测试问题

id | name       | test_id
---+------------+--------
1  | question 1 | 1
2  | question 2 | 1
3  | question 3 | 1
4  | question 4 | 1
5  | question 1 | 2
6  | question 2 | 2
7  | question 3 | 2
8  | question 4 | 2
答案
我把所有测试的答案放在这里

id | question_id
---+-------------
1  | 1
2  | 2
3  | 5
4  | 6
5  | 7
6  | 8
我怎样才能得到一份在一次测试中完成问题百分比的列表。在这种情况下:

name   | percentage
-------+-----------
test 1 | 50
test 2 | 100
试一试

请参见此示例


参见此示例

你从哪里获得百分比?我应该计算它们-100*计数(特定测试的答案)/计数(特定测试的所有答案)你从哪里获得百分比?我应该计算它们-100*计数(特定测试的答案)/计数(特定测试的所有答案)我再次更新。如果你喜欢,你可以使用这个更短的查询。我再次更新了。如果您愿意,可以使用这个更短的查询。
select t.name, 
       cast((count(a.id) / count(q.id)) * 100 as unsigned) as percentage
from tests t
left outer  join questions q on q.test_id = t.id
left outer  join answers a on a.question_id = q.id
group by t.name