如何从数据库中 select 得票最多的用户?

How to select users with most votes, from a database?

我有一个接受投票的小型系统,在我的查询中,我选择投票(用户名)和该用户的总票数。 我的想法是只显示拥有更多选票的用户,我知道我可以添加 ORDER BY 的查询并获得第一个值,该值将是票数最高的那个。 但是我正在尝试确定两个人是否有票数。我该怎么做?

我正在使用 cgi。

my $connection = $MY_CONNECTION->prepare("SELECT voto, COUNT(*) total FROM votos  WHERE mes = 12 GROUP BY vote HAVING COUNT(*) > 0 ORDER BY COUNT(*)");
$connection->execute || print "ERROR 1";

my @resultados;
my $info = '';


while ($info = $connection->fetchrow_hashref()) {

    my $nombre   = $info->{voto};
    my $totalVotos = $info->{total};

my $winner = "";
my $temp2  = "";
my $temp   = $totalVotos ;

if ($temp2 => $temp) {
    $temp2 = $totalVotos ;
    $winner = $nombre   ; 

}


    print "<p><strong>Winner: </strong> $winner </p> "; 
    print "<hr>";
}

使用 subquery 然后应用 order by,将结果限制为 2 添加 limit 2.

select t1.voto, t1.ct as total from (
    SELECT voto, count(1) ct FROM votos  
    WHERE mes = 12 
    GROUP BY voto
    HAVING COUNT(1) > 1) t1 
order by t1.total desc limit 2

您可以 return 每个获得最高票数(以及多少票)的人:

select voto, count(*) as total from votos
    where mes = 12
    group by voto -- I assume "vote" in your question was typo
    having total = (
        select max(ct) from (
            select voto,count(*) as ct from votos group by voto
        )
    )
    and total > 0 -- from your original but not sure it can fail
;

您也可以使用 rank() 函数。


在不修改 SQL 的情况下,您可以将 perl 更改为:

# ...

# initialisation shouldn't be inside while loop
my $winner = "";
my $temp2  = "";

while ($info = $connection->fetchrow_hashref()) {
    my $nombre = $info->{voto};
    my $totalVotos = $info->{total};

    if ($temp2 < $totalVotos) { # your original "=>" seems wrong
        $temp2 = $totalVotos;
        @winner = $nombre;
    elsif ($temp2 == $totalVotos) {
        push @winner, $nombre;
    }
}

# postprocessing shouldn't be inside while loop
$winner = join(", ", @winner); # or whatever
print "<p><strong>Winner: </strong> $winner </p> "; 
print "<hr>";

我会推荐 rank():

SELECT voto, total
FROM (SELECT voto, COUNT(*) as total,
             RANK() OVER (ORDER BY COUNT(*) DESC) as seqnum
      FROM votos v
      WHERE mes = 12
      GROUP BY voto
     ) v
WHERE seqnum = 1;

请注意 HAVING COUNT(*) > 0 什么都不做。在您的查询中,COUNT(*) 不可能等于或小于 0