I want to select the best scores per user for a specific game.
My current query selects the score, date_saved and username of the results of skill_game with id 1.
SELECT MAX(score) as score, date_saved, users.username FROM results
INNER JOIN users_results ON results.id = users_results.result_id
INNER JOIN users ON users_results.user_id = users.id
WHERE skill_game_id = 1
GROUP BY user_id
ORDER BY score DESC
And the results are:
| score | username | date_saved |
---------------------------------
| 73 | Mark | 2021-09-06 |
| 51 | John | 2018-08-16 |
| 46 | Ryan | 2020-02-20 |
| 43 | Chris | 2019-08-27 |
| 40 | Steven | 2020-07-04 |
Right now the date_saved is not always correct as Mark's 73 score result was actually saved on 2021-11-03. Below the results of Mark:
| score | username | date_saved |
---------------------------------
| 73 | Mark | 2021-11-03 |
| 35 | Mark | 2021-10-29 |
| 24 | Mark | 2021-09-06 |
The GROUP BY statement selects the first row of the group and MAX(score) selects the highest score in the group. I want to be able to select the highest score and select the corresponding date with it but I'm not sure how to do this with MySQL.