php - List with percentage -


i'm trying make percentage of test answers.

i have mysql db 3 tables

table tests

id | name ---+-------- 1  | test 1 2  | test 2 

table questions tests' questions

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 

table answers put answers tests

id | question_id ---+------------- 1  | 1 2  | 2 3  | 5 4  | 6 5  | 7 6  | 8 

how can list percents of done questions within test. in case this:

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

try

select t.name,         cast((count(a.id) / count(q.id)) * 100 unsigned) percentage tests t left outer  join questions q on q.test_id = t.id left outer  join answers on a.question_id = q.id group t.name 

see sqlfiddle example


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -