简体   繁体   English

如何获得mySQL表中具有相同ID的项目计数?

[英]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. 我有一个名为Users_Answers的表,该表保存(duh)用户已回答的内容。

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. 每个答案都有一个指向特定答案的Answer_id列,这意味着,有一个Answers表包含10个答案,其中每个答案都有一个ID(1-10),并且用户选择的任何答案都将通过引用该列进行设置答案栏。

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. 目前,我正在对用户进行非常丑陋的连接,这种方法可以正常工作,但是永远需要并且完全没有必要,因为在这种情况下,我不在乎单个用户选择了什么,而只是在乎总共选择了10个答案中的每一个答案的次数。

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吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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