如何从数据库中选择得票最多的用户?
Posted
技术标签:
【中文标题】如何从数据库中选择得票最多的用户?【英文标题】:How to select users with most votes, from a database? 【发布时间】:2020-03-14 10:40:48 【问题描述】:我有一个接受投票的小系统,在我的查询中,我选择投票(用户名)和该用户的投票总数。 我的想法是只显示有更多投票的用户,我知道我可以添加 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>";
【问题讨论】:
if ($temp2 => $temp)
这看起来不太对。
【参考方案1】:
您可以通过以下方式返回获得最高票数(以及票数)的所有人:
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>";
【讨论】:
非常感谢朋友的帮助【参考方案2】:使用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
【讨论】:
【参考方案3】:我会推荐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
。
【讨论】:
这应该与 sqlite 一起使用吗?它有 rank() 但我不知道有效的语法。 @jhnc 您需要 sqlite 3.25 或更高版本才能使用窗口函数。以上是关于如何从数据库中选择得票最多的用户?的主要内容,如果未能解决你的问题,请参考以下文章