在 MySQL 中统计票数
我有一个表,包含以下列: id name vote
每个名称都是唯一的,每个投票要么为空,要么包含一个名称。
我需要一个 MySQL 语句来返回每个人有多少票,以及谁为每个人投票。
我已经为此工作了 3 个小时,我完全不知所措,所以说实话,我不在乎它的效率有多低,或者你是如何做到的。
I have a table, with the following columns:
id name vote
Each name is unique, and each vote is either null or contains a name.
I need a MySQL statement to return how many votes each person has, and who voted for each person.
I've been working on this for 3 hours, and I'm at a complete loss, so I honestly don't care how inefficient it is, or how you do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SELECT name, count(*) as num_votes, GROUP_CONCAT(vote) as voted_by FROM table GROUP BY 1
SELECT name, count(*) as num_votes, GROUP_CONCAT(vote) as voted_by FROM table GROUP BY 1
多少票:
谁投票了:
......除非我误读了什么,否则应该这么简单
How many votes:
Who voted for each:
... unless I'm misreading something, it should be that simple
要获取每个人的票数:
要获取谁投票给谁,您基本上必须选择整个表,忽略空值
To get number of votes per person:
To get who voted for whom you basically has to select the entire table, leaving out the nulls