Error query pdo (Sort by count) [closed]

1

I'm trying to make a ranking based on the amount of coins in each. In descending order. I used this query in pdo:

$ranking = $pdo->query("SELECT * FROM usuarios WHERE banido='false' GROUP BY usuario ORDER BY count(moedas) DESC LIMIT 3");

Complete code:

<?php
                                $ranking = $pdo->query("SELECT * FROM usuarios WHERE banido='false' GROUP BY usuario ORDER BY count(moedas) DESC LIMIT 3");
                                $i = 1;
                                while($ver = $ranking->fetch(PDO::FETCH_ASSOC)){
                                    if($i == 1){
                                        $class = 'gold';
                                    }
                                    else if($i == 2){
                                        $class = 'silver';
                                    }
                                    else if($i == 3){
                                        $class = 'bronze';
                                    }
                            ?>
                            <div class="registry big <?php echo $class; ?>">
                                <div class="rBox"><i class="fa fa-user-circle" aria-hidden="true"></i>&nbsp;<a href="#"><?php echo $ver['usuario']; ?></a></div>
                                <div class="base">
                                    <div class="avatar" style="background-image: url('https://www.habbo.com.br/habbo-imaging/avatarimage?&user=<?php echo $ver['usuario']; ?>&action=std&direction=3&head_direction=3&img_format=png&gesture=std&headonly=0&size=b')"></div>
                                </div>
                                <div class="comments"><i class="fa fa-tint" aria-hidden="true"></i><br><?php echo $ver['moedas']; ?></div>
                            </div>
                            <?php $i++; } ?>

However, I can not find the error. The first place is correct, but in the second place appears a random user with 0 of coins, and the 3rd with its currencies due, but it was to be in the 2 nd place.

    
asked by anonymous 29.06.2017 / 20:51

1 answer

0

Try adding an alias

SELECT *, COUNT(moedas) AS money
FROM usuarios
WHERE banido='false' GROUP BY usuario
ORDER BY money DESC LIMIT 3;
    
29.06.2017 / 21:11