Counting these Rows, and, Perhaps, Giving Percentages
I've got this table called poll_answers and the columns it has that pertains to this are row_id, poll_id, and answer_id. row_id is the primary key. Whenever somebody makes an answer to a poll, the poll_id and answer_id are inserted into the database. This is how I know which answers go with which poll.
Well, what I want this query to do is go search for the answers for a particular poll, for instance, a poll with a poll_id of 2, and figure out how many times each answer has been given and, if this is possible, what percentage of people gave that answer.
I don't know if this matters, but the number of possible answers a poll can have varies between two and ten.
The better I get at programming, the more I appreciate arrays. Handy dandy things they are.
SELECT answer_id, answer_total, round(answer_total/total_replies*100,2) answer_pct, total_replies
FROM (SELECT Count(answer_id) answer_total, answer_id
WHERE poll_id = 1
GROUP BY answer_id ) a,
(SELECT count(answer_id) total_replies
WHERE poll_id = 1) b;