How do I get a count of items with identical IDs in a mySQL table?
I have a table called Users_Answers which holds (duh) what users have answered.
Each answer has an Answer_id column which points to a specific answer, meaning, there's an Answers table that has 10 answers, where every answer has an id (1-10), and whatever answer the user picks, that column gets set with referencing the Answer column.
What I need to do is get a count of how many times each answer has been picked. At the moment, I'm doing a really ugly join on the users, which works, but takes fo开发者_开发百科r ever and is totally unnecessary, since I don't care, in this instance, what the individual user has selected, but rather just need an aggregate total of how many times each one of the 10 answers has been picked.
I know there's a totally simple and elegant way to get this to happen, but I'm totally stumped.
Thanks in advance.
Try this:
select answer_id, count(*) as total from user_answers group by answer_id
select answer_id, count(*) from Users_Answers group by answer_id
?
精彩评论