在 MySQL 中统计票数

发布于 2024-12-20 09:15:39 字数 173 浏览 5 评论 0原文

我有一个表,包含以下列: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

安静被遗忘 2024-12-27 09:15:39

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

晨曦慕雪 2024-12-27 09:15:39

多少票:

select 
 count(*) as numVotes, 
 vote
from
 voteTable
where
 vote IS NOT NULL
group by 
 vote
order by
 numVotes desc

谁投票了:

Select
 name,
 vote
from
 voteTable

......除非我误读了什么,否则应该这么简单

How many votes:

select 
 count(*) as numVotes, 
 vote
from
 voteTable
where
 vote IS NOT NULL
group by 
 vote
order by
 numVotes desc

Who voted for each:

Select
 name,
 vote
from
 voteTable

... unless I'm misreading something, it should be that simple

春夜浅 2024-12-27 09:15:39
select count(name), id from your_table where vote is not null group by (name) 
select count(name), id from your_table where vote is not null group by (name) 
人海汹涌 2024-12-27 09:15:39

要获取每个人的票数:

select vote, 
       count(*) as nbr_of_votes 
  from table 
 where vote is not null 
 group by vote 
 order by nbr_of_votes desc;

要获取谁投票给谁,您基本上必须选择整个表,忽略空值

select vote, 
       name 
  from table
 where vote is not null 
 order by vote;

To get number of votes per person:

select vote, 
       count(*) as nbr_of_votes 
  from table 
 where vote is not null 
 group by vote 
 order by nbr_of_votes desc;

To get who voted for whom you basically has to select the entire table, leaving out the nulls

select vote, 
       name 
  from table
 where vote is not null 
 order by vote;
情深已缘浅 2024-12-27 09:15:39
SELECT VOTE, COUNT(1) Number_Of_Votes,
GROUP_CONCAT(Name)
WHERE VOTE is Not NULL
GROUP BY VOTE
SELECT VOTE, COUNT(1) Number_Of_Votes,
GROUP_CONCAT(Name)
WHERE VOTE is Not NULL
GROUP BY VOTE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文