在MySQL中计票

时间:2011-12-09 22:22:39

标签: mysql count duplicates vote

我有一张表,其中包含以下列: id name vote

每个名称都是唯一的,每个投票都是null或包含名称。

我需要一个MySQL声明来返回每个人有多少票,以及谁投票给每个人。

我已经做了3个小时的工作,而且我完全失去了,所以老实说,我并不关心它是多么低效,或者你是如何做到的。

5 个答案:

答案 0 :(得分:3)

SELECT name, count(*) as num_votes, GROUP_CONCAT(vote) as voted_by FROM table GROUP BY 1

答案 1 :(得分:2)

多少票:

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

谁投票赞成每一个:

Select
 name,
 vote
from
 voteTable

......除非我误读了某些内容,否则应该是那么简单

答案 2 :(得分:2)

select count(name), id from your_table where vote is not null group by (name) 

答案 3 :(得分:1)

获得每人的票数:

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;

答案 4 :(得分:0)

SELECT VOTE, COUNT(1) Number_Of_Votes,
GROUP_CONCAT(Name)
WHERE VOTE is Not NULL
GROUP BY VOTE