SQL查询获取排序表中的行数

时间:2019-05-02 19:33:52

标签: mysql sql

我有这个查询:

SET @playerRank = 0;

SELECT *, @playerRank := @playerRank + 1 AS Rank FROM

( SELECT
  P.name AS nome,
  (playerMMR_RT(P.pid) - DecayTimeDiffRT(P.pid)) AS MMR,
  P.pid

FROM
players_rt P

 ) AS TableData

ORDER BY
MMR DESC,
nome ASC

请注意,playerMMR_RTDecayTimeDiffRT返回两个整数值,但这并不重要。结果是这样的:

enter image description here

但是我想要这个输出:

enter image description here


基本上,结果很好,因为行按MMR排序。我希望等级为1、2、3 ...,但似乎它也按行排序。

我该如何解决?我需要对表进行排序,并且排序表中的排名要升至1、2、3...。

1 个答案:

答案 0 :(得分:1)

尝试在子查询内部移动订单

SET @playerRank = 0;

SELECT *, @playerRank := @playerRank + 1 AS Rank 
FROM ( 
  SELECT
  P.name AS nome,
  (playerMMR_RT(P.pid) - DecayTimeDiffRT(P.pid)) AS MMR,
  P.pid
FROM players_rt P
ORDER BY MMR DESC, nome ASC
 ) AS TableData
ORDER BY rank

或者尝试在排序结果上应用排名

SET @playerRank = 0;

SELECT t.*, @playerRank := @playerRank + 1 AS Rank 
from  ( 
  select *
  FROM ( 
    SELECT
    P.name AS nome,
    (playerMMR_RT(P.pid) - DecayTimeDiffRT(P.pid)) AS MMR,
    P.pid
  FROM players_rt P
   ) AS TableData
  ORDER BY MMR DESC, nome ASC
) t 
 order by rank