MYSQL calculate the percentage chosen

3

I'm doing a QUIZ and at some point I have to show the percentage of the alternatives chosen by the users. I have this table:

ID RESPOSTA
1  a
2  a
3  b
4  b
5  c
6  a

The answer I need to have from MYSQL would be the percentages of choice:

a - 50% b - 33.33% c - 16.6%

How to do this? Tried with GROUP BY, but did not roll, any suggestions?

    
asked by anonymous 15.08.2016 / 16:41

1 answer

6

I do not know if this would be the best way to do this, but I would do it like this:

SELECT count(q.id) / t.total * 100 as perc, resposta from quiz q,
( SELECT count(*) as total from quiz ) t
GROUP BY q.resposta
    
15.08.2016 / 17:02