排序另一个查询的最高值

时间:2018-04-07 15:51:08

标签: mysql sql sorting ranking

获取上个月玩家的统计值我使用此查询:

SELECT COUNT(*) FROM test WHERE player='player1' AND statistic=0 AND time > DATE_SUB(now(), INTERVAL 1 MONTH)

现在我想要价值最高的前十名球员 例如:
player1的值为40(来自上面的查询)
player2的值为78(来自上面的查询)
player3的值为21(来自上面的查询)

现在排名必须是:

  1. player2
  2. PLAYER1
  3. player3
  4. 但我怎样才能对排名进行排序?因为我需要上面查询的值?
    感谢

1 个答案:

答案 0 :(得分:1)

这是你想要的吗?

SELECT (@rn := @rn + 1) as ranking, p.*
FROM (SELECT player, COUNT(*) as cnt
      FROM test
      WHERE statistic=0 AND time > DATE_SUB(now(), INTERVAL 1 MONTH)
      GROUP BY player
      ORDER BY COUNT(*) desc
     ) p CROSS JOIN
     (SELECT @rn := 0) params
LIMIT 10;