Skip to content

Commit

Permalink
refactor: #4 leader board query with join :)
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksesi committed Apr 10, 2022
1 parent 4548109 commit 9ee7311
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public static function get_top(int $limit): array
$table = self::$table;
$db = self::connection();

$query = $db->prepare("SELECT users.*,(SELECT COUNT(*) FROM game_logs WHERE user_id=users.id) AS level_count FROM $table ORDER BY level_count DESC LIMIT $limit");
$query = $db->prepare(
"SELECT users.*, COUNT(game_logs.id) as level_count FROM $table inner join game_logs on users.id=game_logs.user_id GROUP BY users.id ORDER BY `level_count` DESC LIMIT $limit"
);

if (!$query->execute()) {
return false;
Expand Down

0 comments on commit 9ee7311

Please sign in to comment.