Search code examples
mysqlsqlperlcgi

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


I have a small system that accepts votes, on my query I'm selecting the vote(name of user) and the total of votes for this user. My Idea is to only display the user with more votes, with the query that i have I know that i can add ORDER BY and get the first value which will be the one with the highest umber of votes. But i'm trying to identifie if two persons have the number of votes. any ideas how can i do that?

I'm using 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>";
}

Solution

  • You can return everyone who has the highest number of votes (and how many votes) with:

    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
    ;
    

    You may also be able to use a rank() function.


    Without modifying your SQL, you could change the perl to something like:

    # ...
    
    # 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>";