2021-02-26, 11:54 AM
(2021-02-26, 05:22 AM)Kwarde Wrote: If you're using MySQL you can very easily select a top 5 based on certain data. For example, let's say we have this users table:\
Code:+---+----------+------+-------+------
| id | name? ? ? | kills | deaths | score |
+---+----------+------+-------+------
|? 1 | Kwarde? ? |? ? 16 |? ? 25 |? ? 4 |
|? 2 | Playa? ? |? ? 2 |? ? 19 |? ? 1 |
|? 3 | Z3fRaN? ? |? ? 28 |? ? ? 1 |? 100 |
|? 4 | Narf3z? ? |? ? 1 |? ? 28 |? ? 0 |
|? 5 | MathPi? ? |? ? 99 |? ? ? 0 |? ? 50 |
|? 6 | Slayer? ? |? ? 66 |? ? 13 |? ? 25 |
|? 7 | JimmyPage |? ? 0 |? ? ? 2 |? ? 0 |
+---+----------+------+-------+------
Using this query:
Code:SELECT name, kills FROM users ORDER BY kills DESC LIMIT 5;
This would select columns "name" and "kills" from the table "users" and order by kills, descending (DESC -- ASC would order ascending, and thus beginning with the lowest values). LIMIT 5 says: Limit to 5 results. The query itself should be pretty clear.
This would give this output:
Code:+-------+------
| name? | kills |
+-------+------
| MathPi |? ? 99 |
| Slayer |? ? 66 |
| Z3fRaN |? ? 28 |
| Kwarde |? ? 16 |
| Playa? |? ? 2 |
+-------+------
Going through all user files is really unnecessary. This would need only 5 rows as the checking who has more kills would still be checked on the server live. There is no need to go through all user files.