使用最小时间总和获得最大配对用户分数

时间:2013-05-02 18:06:32

标签: mysql sql sum

我有MySQL表,看起来像这样

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

我需要查询它,以便总结每场比赛的得分和时间,所以它看起来像这样

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解决的

提前致谢。

2 个答案:

答案 0 :(得分: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

OUTPUT。

 USER1_ID   USER2_ID    SCORE   TIME
   1            2         3      30

答案 1 :(得分: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}我认为1 and 2 1的user_id是他首先出现在他的对中。

第二个查询基于“echo_me”答案以及user,该row_numberranking scores指定user,最高score每个用户的排名为1。

SQLFIDDLE

希望这是有帮助的