简体   繁体   中英

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 for 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.

尝试这个:

select answer_id, count(*) as total from user_answers group by answer_id

select answer_id, count(*) from Users_Answers group by answer_id吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM