如何从php和mysql的统计数据中获得玩家排名

时间:2010-06-04 22:13:26

标签: php mysql

我有以下代码,我试图从mysql获得播放速度等级。

Mysql表调用帐户

 Username | Speed
 -----------------
 Player1     21
 Player2     52
 Player3     33   

 Player2 52(Ranked:1)
 Player3 33(Ranked:2)
 Player2 21(Ranked:3)

 $result = mysql_query("SELECT * FROM accounts") or die (mysql_error());
 while($row = mysql_fetch_array($result)) {
          $username= $row[username];
 $speed = $row['speed'];
  }

抱歉,我一直在尝试不同的方式,但我无法让它工作

3 个答案:

答案 0 :(得分:2)

为什么不简单:

$result = mysql_query("SELECT * FROM accounts ORDER BY speed DESC") or die (mysql_error());

$rank = 1;
while($row = mysql_fetch_array($result)) {
    $username= $row[username];
    $speed = $row['speed'];
    $rank++;
}

答案 1 :(得分:1)

按速度排序:

$result = mysql_query("SELECT * FROM accounts ORDER BY speed DESC") or die (mysql_error());
$prev_rank = 0;
 while($row = mysql_fetch_array($result)) {
          $username= $row['username'];
          $speed = $row['speed'];
          $rank = ++$prev_rank;
  }

了解ORDER BY

答案 2 :(得分:1)

使用它:

$result = mysql_query("SELECT * FROM accounts ORDER BY speed DESC") or die (mysql_error());