0

我有看起来像这样的 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 웃</a> 在这篇文章中解决

提前致谢。

4

2 回答 2

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出现的第一部分首先显示在他们的配对中首先出现的用户,例如:如果有 2 个具有 ID 的用户,我认为是1,因为他首先出现在他的配对中。userspartners1 and 2user_iduser

第二个查询基于“echo_me”答案以及为每个用户row_number指定的ranking,最高的每个用户的排名为 1。scoresuserscore

SQLFIDDLE

希望这会有所帮助

于 2013-05-03T14:37:34.553 回答
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

在这里演示

为了最好的成绩

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

在这里演示

输出。

 USER1_ID   USER2_ID    SCORE   TIME
   1            2         3      30
于 2013-05-02T18:21:41.333 回答