0

I have a big problem with the selection of the table. I want to show me from the highest to the smallest, just that it appears to me like this

NAME               `POINTS`

John Doe            0
John Doe2           432
John Doe3           64
John Doe4           0
John Doe5           138

I want to show from the highest to the smallest and in the database there is someone who has over 432 points just does not show it

 <?php 
$query = sql()->query('SELECT * FROM `PlayersData` WHERE `Points` BETWEEN 0 AND 1000 LIMIT 5');
if( $query->num_rows == 0 ) {
    echo '<div class="nores">No records found.</div>';
} else {
    while( $row = $query->fetch_object() ) {
        $row = (object) $row;
    ?> 
    <div class="row">
                  <div><?php echo $row->SteamID ?></div>
                  <div>
                    <?php echo $row->Points ?>                      </div>
                </div>
    <?php
    }
}

?>

jarlh
  • 42,561
  • 8
  • 45
  • 63
Sebastian
  • 17
  • 3
  • You need to do `ORDER BY Points DESC` to get _from the highest to the smallest_. (After WHERE, before LIMIT.) – jarlh Dec 05 '19 at 20:43
  • @jarlh I tried to do this only because it shows me those who have points below 100 having others with values greater than these – Sebastian Dec 05 '19 at 20:46
  • I don't understand what you mean? BTW, why the between 0 and 1000 condition? Don't you want users with more than 1000 points? – jarlh Dec 05 '19 at 20:51

1 Answers1

3

Add an ORDER BY, use DESC to get the highest values first.

SELECT * FROM `PlayersData` WHERE `Points` BETWEEN 0 AND 1000
ORDER BY Points DESC 
LIMIT 5

If the data type isn't integer, cast it when ordering:

SELECT * FROM `PlayersData` WHERE `Points` BETWEEN 0 AND 1000
ORDER BY cast(Points as integer) DESC 
LIMIT 5
jarlh
  • 42,561
  • 8
  • 45
  • 63