【发布时间】:2009-09-13 04:35:45
【问题描述】:
我想查找行的排名/编号。我不确定我是否解释得很好,所以我会尝试。
我有问题
$sql = 'SELECT SUM(amount) AS total FROM sales ORDER BY total DESC';
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)) {
// go through and print each row biggest 'total' first
echo $row['total'] . '<br />';
}
现在我想根据最大的“总数”为“1”给每个人一个排名。
所以我可以用 php 做一些计数:
$sql = 'SELECT SUM(amount) AS total FROM sales ORDER BY total DESC';
$res = mysql_query($sql);
$rank = 1;
while($row = mysql_fetch_array($res)) {
// go through and print each row biggest 'total' first
echo 'rank: ' . $rank . ', ' . $row['total'] . '<br />';
$rank = $rank + 1;
}
这很好并且有效。但是我想要做的是能够在没有 php 的情况下确定行的排名,这样我就可以根据销售表中的会员 ID 进行 sql 查询。
例如,我有 100 行销售数据,每行都有一个关联 ID,我将如何简单地根据总数最大的关联获得排名?
【问题讨论】: