简体   繁体   English

查找具有特定条件的最大值并检查同一组是否有更高/更低或任何值

[英]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.这适用于JOINGROUP 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 中,您可以使用窗口函数来:

  • rank the scores of a player for each game: ROW_NUMBER() OVER(PARTITION BY player, game ORDER BY score DESC)对每个游戏的玩家得分进行排名: ROW_NUMBER() OVER(PARTITION BY player, game ORDER BY score DESC)
  • count how many times a player played each game: COUNT(*) OVER(PARTITION BY player, game)计算玩家每局游戏的次数: COUNT(*) OVER(PARTITION BY player, game)
  • compute the top score of a given game amongst all players : 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;

Demo on DB Fiddle .DB Fiddle 上的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查字符串的值是高于还是低于php中的另一个字符串 - Check if string's value is higher or lower than another string in php 找到一列的最大值,然后按同一表中的另一列分组 - Find the max value of a column, then group by another column in same table 按对象数组中的值从高到低排序 - Sorting by value higher to lower in array of objects 在php中查找特定多维数组值的max() - find max() of specific multidimensional array value in php PHP 获取键值或最大值更低 - PHP get key value OR max lower 如何通过使用CodeIgniter从mysql数据库中查找和分组特定字段的所有相同值 - How to find and group all same value of a specific field from a mysql database by using CodeIgniter 为几列从一组相同的组中选择平均值,另一组为最大值 - selecting average value from group of same with max on another for several columns 如何在laravel中按组查找更大的值并查找元素数并使之更高 - How to find greater value by group by and also find element count and make it higher in laravel PHP MySQLi numrow输出1,即使该值大于或小于1 - PHP MySQLi numrows outputs 1 even if the value is higher or lower than 1 域驱动设计,映射器/存储库值高于/低于 - Domain driven design, mapper/repository value is higher/lower than
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM