如何使用MySQL从复杂的查询中获取条目的位置?

时间:2019-02-02 09:48:09

标签: mysql sql rank

我想从排行榜上知道玩家的位置(排名)。我没有排行榜表,它是由users表和一个查询生成的。这是我获取条目的方式:

SELECT
   u.id as userId,
   u.xp as xp,
   u.nickname as nickname,
   SUM(opr.games_won) as wonGames,
   SUM(opr.games_lost) as lostGames 
FROM
   users u 
   INNER JOIN
      opponent_player_result opr ON u.id = opr.user_id 
WHERE
   u.last_login >= 1548374400000 
   AND u.xp > 0 
   AND u.zone_id = 1 
GROUP BY
   u.id 
ORDER BY
   u.xp DESC,
   wonGames DESC,
   lostGames ASC
LIMIT 0, 50;

xp列用于存储玩家的经验值。每玩完游戏,此列就会更新。

opponent_player_result是一个单独的表格,我在其中保留玩家的游戏结果:

+----+------------+-----------+-----------------+---------+
| id | games_lost | games_won | opponent_player | user_id |
+----+------------+-----------+-----------------+---------+
|  1 |          3 |         0 |               0 |       1 |
|  2 |          2 |         1 |               1 |       1 |
|  3 |          0 |         3 |               2 |       1 |
|  4 |          4 |         2 |               0 |       2 |
|  5 |          0 |         1 |               1 |       2 |
|  6 |          1 |         1 |               2 |       2 |
|  7 |          2 |         3 |               0 |       3 |
|  8 |          3 |         0 |               1 |       3 |
|  9 |          3 |         4 |               2 |       3 |
+----+------------+-----------+-----------------+---------+

您可能已经注意到,每个玩家都有3个对手玩家结果(因为该玩家只能玩3个机器人)。

上面的查询将给我以下结果:

+--------+----+----------+----------+-----------+
| userId | xp | nickname | wonGames | lostGames |
+--------+----+----------+----------+-----------+
|      1 | 34 | nick1    |        4 |         5 |
|      3 | 29 | nick3    |        7 |         8 |
|      2 | 29 | nick2    |        4 |         5 |
+--------+----+----------+----------+-----------+

我没有使用MySQL RANK()函数,也没有增加变量以关联玩家位置(也许那是我应该做的)。

如何从上述排行榜结果中获得单个玩家的位置?

例如,具有#id 3的用户的排名为2

这是我正在努力寻找排行榜中玩家位置的查询:

SELECT u.id AS userId,
       u.xp AS xp,
       1 +
  (SELECT COUNT(*)
   FROM
     (SELECT SUM(opr.games_won) AS wonGames,
             SUM(opr.games_lost) AS lostGames
      FROM users AS p
      INNER JOIN opponent_player_result AS opr ON p.id = opr.user_id
      WHERE p.last_login >= 1548374400000
        AND p.xp > 0
        AND p.xp > u.xp
        AND p.zone_id = 1
      GROUP BY p.id
      ORDER BY p.xp DESC, wonGames DESC, lostGames ASC) AS counter) AS ranking
FROM users AS u
WHERE u.id = 1;

但是我遇到以下错误:

  

错误1054(42S22):“ where子句”中的未知列“ u.xp”

据我所知,MySQL仅允许1层深度的嵌套或子查询,但在我的示例中,我为3层深度。但是,如何从子查询中引用外部查询呢?

这是获得排行榜的正确方法吗?我应该创建一个单独的表并定期更新它吗?我非常担心会降低性能。

我正在使用MySQL 8.0版本。

1 个答案:

答案 0 :(得分:2)

使用MySQL 8.0的排名功能很简单。例如,DENSE_RANK计算分区中每一行的排名(并列记录分配相同的排名)。

SELECT
   u.id as userId,
   u.xp as xp,
   u.nickname as nickname,
   SUM(opr.games_won) as wonGames,
   SUM(opr.games_lost) as lostGames,
   DENSE_RANK() OVER(ORDER BY u.xp DESC) rnk
FROM
   users u 
   INNER JOIN
      opponent_player_result opr ON u.id = opr.user_id 
WHERE
   u.last_login >= 1548374400000 
   AND u.xp > 0 
   AND u.zone_id = 1 
GROUP BY
   u.id, u.xp, u.nickname
ORDER BY
   u.xp DESC,
   wonGames DESC,
   lostGames ASC
LIMIT 0, 50;

Demo on DB Fiddle

| userId | xp  | nickname | wonGames | lostGames | rnk |
| ------ | --- | -------- | -------- | --------- | --- |
| 1      | 34  | nick1    | 4        | 5         | 1   |
| 3      | 29  | nick3    | 7        | 8         | 2   |
| 2      | 29  | nick2    | 4        | 5         | 2   |