I have a MySQL table containing userID,score .
A single user can have many scores, but there can be many users, of course.
I want to retrieve the highest score for-each of the userID‘s in the database.
I’ve tried the following but I feel like i’m in the wrong way :
SELECT DISTINCT(`userID`), `score` FROM `myTable` ORDER BY `score` DESC
Any assistance will be greatly appreciated.
Thanks
Shai.
You want the aggregate function
maxwith thegroup byclause:The
group bysays, “Hey, MySQL, get me the maxscore, but partition it byuserid.” This way, you get the max score for eachuserid.Additionally, you can
order bythe aliased column so you get the list of users by max score, descending (for a leaderboard or what have you).