嗨,我試圖根據$ fratio對這個表進行排序。具有最高美元值的人將被置於表中的第一位。下面是我到目前爲止工作的代碼 - 你可以嘗試使用基於一個特定的字段對php表進行排序
ORDER BY kills/deaths DESC
所以完整的查詢看起來像
SELECT `UserID`,`Playername`,`Kills`,`Deaths` FROM users ORDER BY kills/deaths DESC LIMIT 0,50
而且完整的代碼看起來像這樣
// MySQL connection.
$connection = mysql_connect($host,$username,$password);
@mysql_select_db($database) or die("Unable to select database. Be sure the databasename exists and online is.");
$query="SELECT `UserID`,`Playername`,`Kills`,`Deaths` FROM users LIMIT 0,50";
$query = mysql_query($query);
echo('<table width="300" border="2" cellspacing="3" cellpadding="3">
<tr>
<td style="min-width:150px;">Playername:</td>
<td style="width:100px">Kills:</td>
<td style="width:100px">Deaths:</td>
<td style="width:100px">Ratio:</td>
</tr>');
while($row = mysql_fetch_assoc($query))
{
$id = $row['UserID'];
$playername = $row['Playername'];
$kills = $row['Kills'];
$deaths = $row['Deaths'];
$ratio = ($kills/$deaths);
$fratio = ceil($ratio);
echo('
<tr>
<td style="min-width:150px;"><a href="stats.php?id='.$id.'">'.$playername.'</a></td>
<td style="width:100px">'.$kills.'</td>
<td style="width:100px">'.$deaths.'</td>
<td style="width:100px">'.$fratio.'</td>
</tr>');
}
echo('</table>');
mysql_close($connection);
?>
MYSQL * ORDER BY * – 2012-01-06 05:13:21