MySQL Rank in the Case of Ties

后端 未结 2 1045
南笙
南笙 2021-01-16 16:40

I need some help dealing with ties when ranking in MySQL. For example:

PLAYER | POINTS

  • Mary: 90
  • Bob: 90
  • Jim: 65
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-16 17:33

    SELECT players.*, COUNT(higher_ranking.id) + 1 AS rank
        FROM players
        LEFT JOIN players AS higher_ranking
            ON higher_ranking.points > players.points
        GROUP BY players.id
    

    On Postgres, you could use window functions RANK() to achieve this, which is much nicer. I don't know of anything like that for MySQL.

提交回复
热议问题