想要调用排名位置,@ curRank是否可能?

我创建了一个排名表

    SELECT `item`, (`totalrate` / `nrrates`) AS `rank`, @curRank := @curRank
+ 1 AS `ranking`  FROM `rtgitems`, (SELECT

    @curRank := 0) r WHERE item
    REGEXP 'Total'
     ORDER BY (`totalrate` / `nrrates`)  DESC


我有桌子

item       rank      ranking

Karla       9.5       1
Kelly       9.3       2
Arian       9.1429    3


在kelly页面中,我想调出她的排名位置

    SELECT `item`, (`totalrate` / `nrrates`)
AS `rank`, @curRank := @curRank + 1 AS `ranking`
FROM `rtgitems`,
(SELECT @curRank := 0) r WHERE item REGEXP 'kelly'
ORDER BY (`totalrate` / `nrrates`) DESC LIMIT 10
echo "<td align='center' width='250'>" . $row['ranking'] . "</td>";


但它只给我1而不是2

最佳答案

您可以将WHERE子句从排名计算结果中移出。

例:

SELECT * FROM (
    SELECT
        `item`, (`totalrate` / `nrrates`) AS `rank`,
        @curRank := @curRank + 1 AS `ranking`
    FROM `rtgitems`, (SELECT @curRank := 0) r
    ORDER BY (`totalrate` / `nrrates`) DESC
    LIMIT 10
) results
WHERE item REGEXP 'kelly'

关于php - 如何从@curRank获取排名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23033051/

10-16 13:36