[英]Find max value with specific criteria and check if there is higher/lower or any value for same group
I am trying to get game... and max score for user and information is there higher/lower value(score of other players) than user's max value.我正在尝试获取游戏...用户和信息的最大分数是否高于/低于用户的最大值(其他玩家的分数)。 I will explain situation.
我会解释情况。
User should get his games and max score for each game WHERE game is active.用户应该在每个游戏活跃的地方获取他的游戏和最大分数。 That works fine with
JOIN
and GROUP BY
but I have no idea how to get information is there more scores and are they higher or lower than users max score.这适用于
JOIN
和GROUP BY
但我不知道如何获取信息是否有更多分数以及它们是否高于或低于用户最大分数。
Table 1: scores
表 1:
scores
+----+--------+------+--------+-------+
| id | gameID | game | player | score |
+----+--------+------+--------+-------+
| 1 | 1 | pang | lea | 50 |
| 2 | 1 | pang | lea | 60 |
| 3 | 1 | pang | paola | 70 |
| 4 | 2 | pong | lea | 100 |
| 5 | 2 | pong | paola | 90 |
+----+--------+------+--------+-------+
Table 2: games
表 2:
games
+----+------+--------+
| id | name | active |
+----+------+--------+
| 1 | pang | yes |
| 2 | pong | yes |
| 3 | pung | yes |
+----+------+--------+
Code:代码:
$loggedUser = 'lea';
$sql =
"
SELECT s.gameID
, s.game
, COUNT(s.id) c
, MAX(s.score) Max
FROM scores s
RIGHT
JOIN games g
ON s.gameID = g.id
WHERE g.active = 'yes'
AND s.player = '$loggedUser'
GROUP
BY s.gameID
ORDER
BY c
";
Output should look like this:输出应如下所示:
Hello Lea, your top scores:
你好 Lea,你的最高分:
pang played x 2, max score 60 pts (top score is 70)....
pang 玩了 x 2,最高得分 60 分(最高得分为 70)...
pong played x 1, 100 pts , you have higher score....
pong 打了 x 1, 100 pts , 你有更高的分数....
In MySQL 8.0, you can use window functions to:在 MySQL 8.0 中,您可以使用窗口函数来:
ROW_NUMBER() OVER(PARTITION BY player, game ORDER BY score DESC)
ROW_NUMBER() OVER(PARTITION BY player, game ORDER BY score DESC)
COUNT(*) OVER(PARTITION BY player, game)
COUNT(*) OVER(PARTITION BY player, game)
MAX(score) OVER(PARTITION BY game ORDER BY score DESC)
MAX(score) OVER(PARTITION BY game ORDER BY score DESC)
Consider the following query:考虑以下查询:
SELECT game_name, cnt games_played, score max_score, top_score
FROM (
SELECT
g.name game_name,
s.player,
s.score,
ROW_NUMBER() OVER(PARTITION BY player, game ORDER BY score DESC) rn,
COUNT(*) OVER(PARTITION BY player, game) cnt,
MAX(score) OVER(PARTITION BY game ORDER BY score DESC) top_score
FROM scores s
INNER JOIN games g ON g.id = s.gameID AND g.active = 'yes'
) x WHERE rn = 1 AND player = 'lea';
In this db fiddle with your sample data, the query yields:在这个 db fiddle与您的示例数据中,查询产生:
| game_name | games_played | max_score | top_score |
| --------- | ------------ | --------- | --------- |
| pang | 2 | 60 | 70 |
| pong | 1 | 100 | 100 |
In earlier versions of MySQL, one solution would be to use an aggregated subquery to compute the top score for each game amongst all players, and the JOIN
it with the main aggregated query:在早期版本的 MySQL 中,一种解决方案是使用聚合子查询来计算所有玩家中每场比赛的最高分,并将其与主聚合查询
JOIN
:
SELECT g.name game_name, COUNT(*) games_played, MAX(s.score) max_score, ts.top_score
FROM scores s
INNER JOIN games g
ON g.id = s.gameID AND g.active = 'yes'
INNER JOIN (SELECT gameID, MAX(score) top_score FROM scores GROUP BY gameID) ts
ON s.gameID = ts.gameID
WHERE s.player = 'lea'
GROUP BY s.player, g.id, g.name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.