我有MySql表,看起来像这样
scores
game_id | level | score | time
--------+-------+-------+-----
1 | 1 | 1 | 10
1 | 2 | 0 | 10
1 | 3 | 1 | 20
2 | 1 | 1 | 5
2 | 2 | 1 | 15
2 | 3 | 0 | 10
3 | 1 | 0 | 10
3 | 2 | 0 | 10
3 | 3 | 0 | 10
games
game_id | user_id
--------+--------
1 | 1
2 | 1
3 | 2
我需要查询它,以便总结每场比赛的得分和时间,所以它看起来像这样
game_id | user_id | sumPoints | sumTime
--------+---------+-----------+--------
1 | 1 | 2 | 40
2 | 1 | 2 | 30
3 | 2 | 0 | 30
而且我需要为每个用户获得最佳分数(最高分我的意思是max(sumPoints)和min(sumTime),所以看起来像这样:
game_id | user_id | sumPoints | sumTime
--------+---------+-----------+--------
2 | 1 | 2 | 30
3 | 2 | 0 | 30
这是最终的结果。我可以在点数和时间相加的情况下做一些事情,但我不知道如何才能获得每位用户的最高分数。 我想知道,我怎么能通过SQL查询来做到这一点? 提前致谢
答案 0 :(得分:1)
下面的查询将为您提供部分结果。
SELECT a.game_ID,
b.user_id,
SUM(a.Score) sumPoint,
SUM(a.time) sumTime
FROM scores a
INNER JOIN games b
ON a.game_ID = b.game_ID
GROUP BY a.game_ID, b.user_id
输出
╔═════════╦═════════╦══════════╦═════════╗
║ GAME_ID ║ USER_ID ║ SUMPOINT ║ SUMTIME ║
╠═════════╬═════════╬══════════╬═════════╣
║ 1 ║ 1 ║ 2 ║ 40 ║
║ 2 ║ 1 ║ 2 ║ 30 ║
║ 3 ║ 2 ║ 0 ║ 30 ║
╚═════════╩═════════╩══════════╩═════════╝
由于MySQL
不像任何其他RDBMS一样支持Windows函数,因此产生所需结果可能会很麻烦。
SELECT a.*
FROM
(
SELECT a.game_ID,
b.user_id,
SUM(a.Score) sumPoint,
SUM(a.time) sumTime
FROM scores a
INNER JOIN games b
ON a.game_ID = b.game_ID
GROUP BY a.game_ID, b.user_id
) a
INNER JOIN
(
SELECT user_ID,
MIN(sumTime) sumTime
FROM
(
SELECT a.game_ID,
b.user_id,
SUM(a.Score) sumPoint,
SUM(a.time) sumTime
FROM scores a
INNER JOIN games b
ON a.game_ID = b.game_ID
GROUP BY a.game_ID, b.user_id
) s
GROUP BY user_ID
) b ON a.user_id = b.user_id AND
a.sumTime = b.sumTime
输出
╔═════════╦═════════╦══════════╦═════════╗
║ GAME_ID ║ USER_ID ║ SUMPOINT ║ SUMTIME ║
╠═════════╬═════════╬══════════╬═════════╣
║ 2 ║ 1 ║ 2 ║ 30 ║
║ 3 ║ 2 ║ 0 ║ 30 ║
╚═════════╩═════════╩══════════╩═════════╝
答案 1 :(得分:0)
JW的答案与我的答案基本相同。我只是觉得这么早就把用户带进来似乎不必要地使事情变得复杂。怎么样......
SELECT x.*
, u.user_id
FROM
( SELECT game_id
, SUM(score) ttl_score
, SUM(time) ttl_time
FROM scores
GROUP
BY game_id
) x
JOIN
( SELECT ttl_score
, MIN(ttl_time) min_ttl_time
FROM
( SELECT game_id
, SUM(score) ttl_score
, SUM(time) ttl_time
FROM scores
GROUP
BY game_id
) a
GROUP
BY ttl_score
) y
ON y.ttl_score = x.ttl_score
AND y.min_ttl_time = x.ttl_time
JOIN games g
ON g.game_id = x.game_id;