我有以下表格:
游戏:
+----+-----------------------------------+ | id | title | +----+-----------------------------------+ | 1 | The Witcher | | 2 | The Witcher 2: Assassins of Kings | | 3 | The Witcher 3: Wild Hunt | +----+-----------------------------------+
平台:
+----+------+ | id | name | +----+------+ | 1 | PC | | 2 | MAC | | 3 | X360 | | 4 | PS3 | | 5 | XONE | | 6 | PS4 | +----+------+
游戏平台
+----+---------+-------------+ | id | id_game | id_platform | +----+---------+-------------+ | 1 | 1 | 1 | | 2 | 1 | 2 | | 3 | 2 | 1 | | 4 | 2 | 2 | | 5 | 2 | 3 | | 6 | 3 | 1 | | 7 | 3 | 5 | | 8 | 3 | 6 | +----+---------+-------------+
结果我想要这样的东西:
+-----------------------------------+----+-----+------+-----+------+-----+
| game | PC | MAC | X360 | PS3 | XONE | PS4 |
+-----------------------------------+----+-----+------+-----+------+-----+
| The Witcher 3: Wild Hunt | x | | | | x | x |
+-----------------------------------+----+-----+------+-----+------+-----+
我正在使用的查询是:
SELECT g.title as 'Title',
IF (gp.id_platform = 1, 'x', '') as 'PC',
IF (gp.id_platform = 2, 'x', '') as 'MAC',
IF (gp.id_platform = 3, 'x', '') as 'X360',
IF (gp.id_platform = 4, 'x', '') as 'PS3'
IF (gp.id_platform = 5, 'x', '') as 'XONE'
IF (gp.id_platform = 6, 'x', '') as 'PS4'
FROM game g LEFT JOIN (platform pl, game_platform gp) ON (g.id = gp.id_game and pl.id = gp.id_platform)
WHERE g.id = 1;
一切都很好,除了数据如下所示:
+-----------------------------------+----+-----+------+-----+------+-----+
| game | PC | MAC | X360 | PS3 | XONE | PS4 |
+-----------------------------------+----+-----+------+-----+------+-----+
| The Witcher 3: Wild Hunt | x | | | | | |
| The Witcher 3: Wild Hunt | | | | | x | |
| The Witcher 3: Wild Hunt | | | | | | x |
+-----------------------------------+----+-----+------+-----+------+-----+
当我GROUP BY
在末尾添加子句时:
SELECT g.title as 'Title',
IF (gp.id_platform = 1, 'x', '') as 'PC',
IF (gp.id_platform = 2, 'x', '') as 'MAC',
IF (gp.id_platform = 3, 'x', '') as 'X360',
IF (gp.id_platform = 4, 'x', '') as 'PS3'
IF (gp.id_platform = 5, 'x', '') as 'XONE'
IF (gp.id_platform = 6, 'x', '') as 'PS4'
FROM game g INNER JOIN (platform pl, game_platform gp) ON (g.id = gp.id_game and pl.id = gp.id_platform)
WHERE g.id = 1
GROUP BY g.title;
我收到错误:
#1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db_klimos.gp.id_platform' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
如果我将 gp.id_platform 添加到GROUP BY
子句中,我将得到初始结果,就好像数据没有分组一样。
有没有一种方法可以在不更改 sql_mode=only_full_group_by 的情况下对表中的数据进行分组?我已经知道此选项的含义以及引入它的原因。我使用的数据库不是自托管的,所以无论如何我都无法将其关闭。