从四个表中选择并将一些结果分组到一列中

Selecting from four tables and grouping some results into one column

sports_games Table

==========================================================================
| game_id | team_id | game_type | time_started | time_ended | planned_by |
|========================================================================|
|   1     |    1    |     1     | 1640799417   | 1641146196 |     1      |
|   2     |    2    |     1     | 1640971535   | 1641579516 |     1      |
|   3     |    1    |     2     | 1640971553   | 1641582723 |     8      |
|   4     |    2    |     2     | 1640971585   | 1641404242 |     9      |
|   5     |    3    |     4     | 1641061431   | 1641754479 |     12     |
==========================================================================

我已经能够将上面的 table 与下面的 table;

结合起来

game_types Table

==============================
| game_type | game_type_name |
|============================|
|     1     |   football     |
|     2     |   baseball     |
|     3     |     golf       |
|     4     |    tennis      |
==============================

与以下SQL

SELECT *
    FROM sport_games
    INNER JOIN game_types
    ON sport_games.game_type = game_types.game_type

然而,我现在发现自己在兜兜转转,一个接一个地尝试可能的解决方案。我需要包含来自另外两个 table 的数据,participentsuser_basic.

participents table 仅包含 game_iduser_id 列,每个参与者一行,两者组合为主键,并且在 user_basic table,你可以找到 user_id 以及他们的 user_name.

对于第一个 table 中的 planned_by 列,这也是一个 user_id 我也想获得他们的用户名。

对于所有参与者,我希望他们在按他们的游戏分组的一栏中。

我尝试过的事情

感觉自己有点不知所措,似乎有进有退两步!然后在其中使用 JOIN,我不确定我的搜索措辞是否正确以使我走上正确的道路,所以我的问题标题现在可能有点模棱两可,抱歉,如果这是案子! - 请提出其他建议,谢谢。

示例期望结果

"2": { // <- game_id
        "game_type": 2,
        "game_type_name": "baseball",
        "participants": {
            "1": "username_1",
            "2": "username_21",
            "3": "username_3",
            "4": "username_4",
        },
        "time_started": 1641061431,
        "time_completed": 1641754479,
        "planned_by": {
            "851730": "username_1",
        }
}

time_ready 是现有列还是从其他列计算得出的?我在提供的任何表格中都没有看到 time_ready

SELECT 
   game_types.game_type
   , game_types.game_type_name
   , participents.user_id
   , user_basic.user_name
   , sports_games.time_started
   , sports_games.time_ended AS time_completed
   , sports_games.planned_by
FROM sports_games
INNER JOIN game_types
   ON sports_games.game_type = game_types.game_type
INNER JOIN participents
   ON sports_games.game_id = participents.game_id
INNER JOIN user_basic
   ON participants.user_id = user_basic.user_id
ORDER BY
   game_types.game_type

您必须正确加入tables和tableuser_basic两次:以获得参与者和策划游戏的用户。

对于 MariaDB 10.5+,您可以使用 JSON_ARRAYAGG():

SELECT sg.game_id, 
       sg.game_type,
       gt.game_type_name,
       JSON_ARRAYAGG(JSON_OBJECT(ub.user_id, ub.user_name)) participants,
       sg.time_started,
       sg.time_ended,
       JSON_OBJECT(sg.planned_by, pb.user_name) planned_by
FROM sports_games sg
INNER JOIN user_basic pb ON pb.user_id = sg.planned_by
INNER JOIN game_types gt ON gt.game_type = sg.game_type
INNER JOIN participents p ON p.game_id = sg.game_id
INNER JOIN user_basic ub ON ub.user_id = p.user_id
GROUP BY sg.game_id;

对于以前的版本使用 GROUP_CONCAT():

SELECT sg.game_id, 
       sg.game_type,
       gt.game_type_name,
       CONCAT('{', GROUP_CONCAT(ub.user_id, ' : ', ub.user_name SEPARATOR ', '), '}') participants,
       sg.time_started,
       sg.time_ended,
       CONCAT('{', GROUP_CONCAT(DISTINCT sg.planned_by, ' : ', pb.user_name), '}') planned_by
FROM sports_games sg
INNER JOIN user_basic pb ON pb.user_id = sg.planned_by
INNER JOIN game_types gt ON gt.game_type = sg.game_type
INNER JOIN participents p ON p.game_id = sg.game_id
INNER JOIN user_basic ub ON ub.user_id = p.user_id
GROUP BY sg.game_id;

参见demo