PHP Mysql排名

时间:2014-12-17 11:45:34

标签: php mysql

我有一个看起来像这样的Mysql数据库:

http://i.imgur.com/bLlXrGg.png

我正在使用这个公式计算排名:

SELECT Name, Kills, Wins, Losses, Killstreak, 
(((Wins * 5) + (Kills / 10) / (Losses / 10)) + ((Wins + Losses) / 50)) +
Killstreak as rank
FROM `hgstats` WHERE `Name` = '$inputPlayer'"

并且输出总和但是我想让它说1,2,3等等,我将如何在查询中执行此操作。

1 个答案:

答案 0 :(得分:1)

在MySQL中,您将使用变量。假设您想要给定玩家的等级,您可以指定所有等级,然后选择您想要的行:

SELECT r.*
FROM (SELECT Name, Kills, Wins, Losses, Killstreak, 
             (((Wins * 5) + (Kills / 10) / (Losses / 10)) + ((Wins + Losses) / 50)) + Killstreak as rankvalue,
             @rn := @rn + 1 as rank
      FROM `hgstats` CROSS JOIN
           (SELECT @rn := 0) vars
      ORDER BY rankvalue
     ) r
WHERE `Name` = '$inputPlayer'"