2011-03-14 111 views
1

我需要一个游戏中排名最高的玩家列表。等级是即时计算的,数据来自两个表格。我设法以正确的方式订购它们,但是@rank:= 0 - > @rank:= @ rank + 1技巧,其中一个名为rank的额外字段正在计算玩家对应特定位置订购参数,不返回正确的等级顺序。这是我有:MySQL排名(排名最高的用户)

SET @rank := 0; 
SELECT 
@overall := u_p_s.exploration + u_p_s.story + u_p_s.collection + u_p.experience AS overall, 
@rank := @rank + 1 AS rank, 
u.nick, u.user_id, u_p_s.exploration, u_p_s.story, u_p_s.collection, u_p.experience 
FROM user AS u 
INNER JOIN user_profile_stats AS u_p_s ON u_p_s.user_id = u.user_id 
INNER JOIN user_profile AS u_p ON u_p.user_id = u.user_id 
ORDER BY overall DESC 
LIMIT 0 ,20; 

结果是正确的整体秩序不正确排序。我总是可以对行进行计数并获得应用程序层的排名,但我仍然需要知道我的查询在这里出了什么问题。如果您有任何关于如何计算特定用户排名的建议,我愿意接受建议。我看到很多类似的问题,但是他们没有一个在飞行中计算排名参数(整体),并且由于我不是MySql最好的,我请您寻求帮助。谢谢。

解决方案:

SET @rank:=0; 
SELECT 
@rank := @rank +1 AS rank, 
nick, user_id, story, overall, collection, experience, exploration 
FROM (
SELECT @overall := u_p_s.exploration + u_p_s.story + u_p_s.collection + u_p.experience AS overall, 
u.nick, u.user_id, u_p_s.story, u_p.experience, u_p_s.collection, u_p_s.exploration 
FROM user AS u 
INNER JOIN user_profile_stats AS u_p_s ON u_p_s.user_id = u.user_id 
INNER JOIN user_profile AS u_p ON u_p.user_id = u.user_id 
ORDER BY overall DESC 
LIMIT 0 , 20 
) AS result 

回答

2

我认为问题是,你计算排序发生之前,首先军衔。

,如果你尝试的水木清华这样的:

SELECT 
@rank := @rank + 1 AS rank, 
overall, u.nick, u.user_id, ... 
FROM (SELECT ... <your query including ORDER BY>) 
+0

肯定的,问题是,我需要一个派生表。谢谢你,这已经很糟糕了。 – chosta 2011-03-14 20:51:20

+0

谢谢......它真的帮了很多.. – 2014-04-22 09:02:24