Skip to content
Advertisement

mysql get ranking position grouped with joined table

I have two tables, the first is players

And another table for games

I would like to have this result

Have anyone idea how to achieve this in mysql? Thank you very much

Advertisement

Answer

You want rank() or row_number():

If two players have the same score, then rank() gives them the same ranking. row_number() will arbitrarily assign them adjacent rankings.

The above works on MySQL 8+. In earlier versions you have two options — subqueries and variables. Here is an example using a correlated subquery:

This is not as efficient as rank(). But with an index on players(player_game_id, player_score) and not too many players per game, then this should have reasonable performance.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement