我有一个球员名单。球员按分数排序。我想知道的是如何获得某个玩家的排名?
到目前为止,这是我的代码(由于它似乎有一些错误,因此无效):
$rank = mysql_query (SET @rank := 0;
SELECT *, @rank := @rank + 1
FROM ava_users
WHERE id = '".$id."'
ORDER BY points DESC);
$rank_res = mysql_fetch_array($rank);
当我尝试使用我的查询时,我收到一条错误消息:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Users/***/Documents/Arcades/Arc_development/arc_projects/***/arc_dev_website/arc_offline/includes/profile/profile_main.inc.php
答案 0 :(得分:1)
user是关键字,因此使用user
来检查参数是否相等。
此外,mysql_query一次只能执行1个查询。
答案 1 :(得分:1)
试试这个:
SELECT `user`, rank
FROM (
SELECT `user`, ( @rank := @rank + 1 ) as rank
FROM ava_users, ( select (@rank := 0 ) ) rnk
ORDER BY points DESC
) ranks
WHERE `user` = '".$id."'
答案 2 :(得分:1)
$rank = mysql_query (
"SELECT a.*,
(
SELECT COUNT(1)
FROM ava_users b
WHERE (b.points, b.id) >= (a.points, a.id)
) AS rank
FROM ava_users a
WHERE a.`user` = '$id'"
);