我对最初来自http://jimmod.com/blog/2008/09/displaying-row-number-rownum-in-mysql/的查询有疑问
SELECT IF(@points=p.points, @rownum, @rownum:=@rownum+1) rank,
p.*, (@points:=p.points) dummy
FROM `ranking` p, (SELECT @rownum:=0) x, (SELECT @points:=0) y
ORDER BY `points` DESC
查询的结果是:
rank user_id name score
-----------------------
1 4 Barney 100
2 2 Ted 88
3 1 Marshall 76
3 3 Robin 76
4 5 Lily 68
如何根据用户 ID 获得相同的排名结果?
下面的查询是基于用户 id 的,但 Robin 的排名将是 4,而不是应该的 3。
SELECT rank FROM (SELECT @rownum:=@rownum+1 rank, p.*
FROM `ranking` p, (SELECT @rownum:=0) r
ORDER BY `points` DESC) a WHERE a.user_id = 3