0

我如何根据一个查询中其他所有人的统计信息来查找一个用户的三个统计信息的排名?

用户 -> 艾伦
等级 -> 25(等级 3)
获胜 -> 347(等级 1)
失败 -> 2(等级 145)

这就是我所拥有的

function get_rank($what,$user) {
$v = mysql_query("
    SELECT username, @curRank := @curRank + 1 AS rank FROM `users`,
    (SELECT @curRank := 0) r
    WHERE id = '".$user."' ORDER BY `".$what."`");
    $a = mysql_fetch_array($v);
        return $v['rank'];
}

它永远不会起作用。

表结构

Id 用户名等级 胜负
1 Alan 25 347 2
2 Joe 34 100 4
3 山姆 12 600 12

谢谢。

4

1 回答 1

1
SELECT * FROM
(SELECT m.name,rank_level,rank_win  ,rank_loss
FROM 
    (SELECT @rank := @rank + 1 AS rank_level,name,id FROM users ORDER BY level DESC) m 
    inner join  (SELECT @rankw := @rankw + 1 AS rank_win,name,id 
        FROM users ORDER BY win DESC) w on m.id=w.id 
    inner join  (SELECT @rankl := @rankl + 1 AS rank_loss,name,id 
        FROM users ORDER BY loss DESC) l on m.id=l.id 
     , (SELECT @rank := 0) m2,(SELECT @rankw := 0) w2,(SELECT @rankl := 0) l2
) k
WHERE k.name= 'Alan';

SQL 小提琴在这里

于 2012-08-09T01:47:29.050 回答