2013-05-02 40 views
0

我有這個樣子獲得最大的配對用戶的得分與分鐘時間總結

users 
user_id | partner_id | name 
--------+------------+----- 
1  | 2   | aaa 
2  | 1   | bbb 
3  | 4   | ccc 
4  | 3   | ddd 

games 
game_id | user_id 
--------+-------- 
1  | 1 
2  | 1 
3  | 2 
4  | 3 
5  | 4 
6  | 4 

scores 
game_id | level | score | time 
--------+-------+-------+----- 
1  | 1  | 1  | 10 
1  | 2  | 1  | 10 
1  | 3  | 1  | 10 
2  | 1  | 0  | 20 
2  | 2  | 0  | 20 
2  | 3  | 0  | 20 
3  | 1  | 1  | 30 
3  | 2  | 1  | 30 
3  | 3  | 1  | 30 
4  | 1  | 1  | 2 
4  | 2  | 1  | 2 
4  | 3  | 1  | 2 
5  | 1  | 1  | 5 
5  | 2  | 1  | 5 
5  | 3  | 1  | 5 
6  | 1  | 1  | 3 
6  | 2  | 1  | 3 
6  | 3  | 0  | 3 

MySQL表,我需要因此總結點和每場比賽的時間來查詢它,所以它看起來像這樣

game_id | user_id | sumPoints | sumTime 
--------+---------+-----------+-------- 
1  | 1  | 3   | 30 
2  | 1  | 0   | 60 
3  | 2  | 3   | 90 
4  | 3  | 3   | 6 
5  | 4  | 3   | 15 
6  | 4  | 2   | 9 

然後,我需要得到每對(其中需要更好的成績一個用戶)的成績最好,所以它看起來是這樣的:

user1_id | user2_id | sumPoints | sumTime 
---------+----------+-----------+-------- 
3  | 4  | 3   | 6 
1  | 2  | 3   | 30 

這是最終結果。我真的很感激,如果有人能告訴我它應該看起來像SQL查詢。 我想提一提,第一部分由JW 웃this post

預先感謝解決。

+0

你怎麼配對的用戶,這是有幫助嗎?或者你的意思是所有可能的用戶配對,用哪個用戶的最高分列出? – Andomar 2013-05-02 18:14:28

+0

用戶之間的關係顯示在表用戶中,其中它顯示具有ID 1的用戶與用戶ID 2 合作並且因爲具有ID 2的用戶與具有ID 1的用戶合作,其顯示第二行 – 2013-05-02 18:27:23

回答

0

像這樣的東西應該工作(這回答你的第二個查詢)

SELECT 
    user_details.user_id, 
    user_details.partner_id, 
    score_details.score, 
    score_details.time 
FROM 
    (SELECT 
     min(user_id) as user_id, 
     max(user_id) as partner_id 
     FROM 
     users 
     GROUP BY 
     user_id + partner_id) AS user_details 
    JOIN 
    (SELECT 
     scores.game_id , 
     games.user_id, 
     sum(score) score, 
     sum(time) time, 
     @row_num := IF(@prev_value=games.user_id,@row_num+1,1) AS row_num, 
     @prev_value := games.user_id 
     FROM 
     scores 
     inner join games on games.game_id = scores.game_id 
     inner join users on users.user_id = games.user_id 
     GROUP BY 
     scores.game_id 
     ORDER BY 
     user_id, 
     score 
    ) as score_details ON (score_details.user_id = user_details.user_id AND score_details.row_num = 1) 

JOIN第一部分相處的users他們partners,首先顯示其對中第一個出現的用戶,如:如果有2個ID爲1 and 2的用戶,我認爲user_iduser 1是他首先出現在他的配對中的。

第二查詢基於一個row_number指定scores的每個userranking,最高score沿着「echo_me」的答案對每個用戶的等級爲1。

SQLFIDDLE

希望

0

試試這個

select scores.game_id ,games.user_id,sum(score) score, sum(time) time 
from scores 
inner join games 
on games.game_id = scores.game_id 
inner join users 
on users.user_id = games.user_id 
group by scores.game_id 

DEMO HERE

最好成績

select users.user_id as user1_id,users.partner_id as user2_id,sum(score) score, sum(time) time 
from scores 
inner join games 
on games.game_id = scores.game_id 
inner join users 
on users.user_id = games.user_id 
group by scores.game_id 
order by sum(time) asc limit 1 

DEMO HERE

輸出。

USER1_ID USER2_ID SCORE TIME 
    1   2   3  30 
+0

好的,看在我編輯頂部, 我不能使用限制,因爲我不知道會有多少對 – 2013-05-02 18:37:54