SQL 将左侧的行组合成一个
SQL Group rows in left join into one
当我将多行合并为一行时,我尝试使用左连接创建查询。我尝试使用 GROUP_CONTENT 函数,但是当我尝试使用它时,我的数据库服务器出现故障。我使用 MariaDB 10.3.17。我有这样的表:
游戏:
game_id game_name
1 Test
2 Stack
3 Other
data_developers:
dev_id dev_name
1 Electronic Arts
2 BioWare
3 2K Games
game_developers
developer_id game_id
1 1
2 1
2 3
我想要的结果:
game_id game_name devs
1 Test Electonics Arts, BioWare
2 Stack 2K Games
我的两个 sql(但没用)
SELECT games.*, GROUP_CONCAT(data_developers.dev_name)
FROM games
LEFT JOIN game_developers ON game_developers.game_id = games.game_id
LEFT JOIN data_developers ON data_developers.dev_id = game_developers.dev_id
LIMIT 500
和第二个查询
SELECT games.*
FROM games
LEFT JOIN game_developers ON game_developers.game_id = games.game_id
LEFT JOIN
(SELECT GROUP_CONCAT(data_developers.developer_name) as developers,
data_developers.developer_id FROM data_developers) x
ON x.developer_id = game_developers.developer_id
当然,也行不通:(
您的查询应该如下所示-
SELECT A.game_id,B.Game_name,GROUP_CONCAT(C.dev_name)
FROM game_developers A
INNER JOIN Games B ON A.game_id = B.game_id
INNER JOIN data_developers C ON A.developer_id = C.dev_id
GROUP BY A.game_id,B.Game_name
当我将多行合并为一行时,我尝试使用左连接创建查询。我尝试使用 GROUP_CONTENT 函数,但是当我尝试使用它时,我的数据库服务器出现故障。我使用 MariaDB 10.3.17。我有这样的表:
游戏:
game_id game_name
1 Test
2 Stack
3 Other
data_developers:
dev_id dev_name
1 Electronic Arts
2 BioWare
3 2K Games
game_developers
developer_id game_id
1 1
2 1
2 3
我想要的结果:
game_id game_name devs
1 Test Electonics Arts, BioWare
2 Stack 2K Games
我的两个 sql(但没用)
SELECT games.*, GROUP_CONCAT(data_developers.dev_name)
FROM games
LEFT JOIN game_developers ON game_developers.game_id = games.game_id
LEFT JOIN data_developers ON data_developers.dev_id = game_developers.dev_id
LIMIT 500
和第二个查询
SELECT games.*
FROM games
LEFT JOIN game_developers ON game_developers.game_id = games.game_id
LEFT JOIN
(SELECT GROUP_CONCAT(data_developers.developer_name) as developers,
data_developers.developer_id FROM data_developers) x
ON x.developer_id = game_developers.developer_id
当然,也行不通:(
您的查询应该如下所示-
SELECT A.game_id,B.Game_name,GROUP_CONCAT(C.dev_name)
FROM game_developers A
INNER JOIN Games B ON A.game_id = B.game_id
INNER JOIN data_developers C ON A.developer_id = C.dev_id
GROUP BY A.game_id,B.Game_name