mySQL “Rank in Highscore”-Query

后端 未结 2 856
滥情空心
滥情空心 2021-01-03 11:52

Hi there coders around the world,

I\'m working on a project where users can do certain things and gain points for it. To simplify this question let\'s say we got 2 t

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-03 12:35

    SELECT  q.*,
            @r := @r + 1 AS rank
    FROM    (
            SELECT  @r := 0
            ) vars,
            (
            SELECT  u.*,
                    SUM(p.points) AS sum_points
            FROM
                    user u
            LEFT JOIN
                    points p
            ON      p.user_id = u.id
            GROUP BY
                    u.id
            ORDER BY
                    sum_points DESC
            ) q
    

提交回复
热议问题