2011-03-14 70 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