I'm unsure how to go about this either because it late, or I'm a MySQL noob (probably both to be honest).
I have a table called 'poll_votes' which comprises of:
ID, poll_id, answer_id, ip, time_posted
What I need to do, is for each unique answer_id in a specific poll_id, calculate the percentage out of the total number of answers that the answer_id counts for. I'm not really good at wording what I need to do, so here is what the query should return (single row):
total -> total number of answers for this poll_id
[0] -> the percentage of 'total' that this makes up eg. 32 (%)
[1] -> the percentage of 'total' that this makes up eg. 16 (%)
[2] -> the percentage of 'total' that this makes up eg. 52 (%)
Obviously, all the percentages added together should equal one hundred. Also to note, that the field names for each percentage returned are not necessarily in any order.
I mean even if the query just returned the total and the number of votes for each answer_id, I'd be able to calculate it easily in PHP.
Could anyone give me any pointers?
Edit: This is what I have so far, but it doesn't work:
SELECT (SELECT COUNT(*) FROM poll_votes) AS total, answer_id, COUNT(answer_id) / total * 100 AS percentage GROUP BY answer_id