【问题标题】:MySQL - error #1055 when joining tables [duplicate]MySQL - 加入表时出现错误 #1055 [重复]
【发布时间】:2017-11-21 12:08:16
【问题描述】:

我有以下表格:

  1. 游戏:

    +----+-----------------------------------+
    | id | title                             |
    +----+-----------------------------------+
    |  1 | The Witcher                       |
    |  2 | The Witcher 2: Assassins of Kings |
    |  3 | The Witcher 3: Wild Hunt          |
    +----+-----------------------------------+
    
  2. 平台:

    +----+------+
    | id | name |
    +----+------+
    |  1 | PC   |
    |  2 | MAC  |
    |  3 | X360 |
    |  4 | PS3  |
    |  5 | XONE |
    |  6 | PS4  |
    +----+------+
    
  3. 游戏平台

    +----+---------+-------------+
    | 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 的情况下对表中的数据进行分组? 我已经知道此选项的含义以及引入它的原因。我使用的数据库不是自托管的,所以无论如何我都无法关闭它。

【问题讨论】:

  • 这被称为数据透视表,这个问题已经在这里被问过很多次了。链接的重复主题描述了 MySQL 中的静态和动态透视。但是,请注意,在应用程序逻辑中执行此类转换可能比在 SQL 中更有效。

标签: mysql pivot-table mysql-error-1055


【解决方案1】:

使用CASE 代替IF 并选择MAX 值以获得所需的结果

试试这个

SELECT g.title as 'Title',
MAX(CASE WHEN gp.id_platform = 1  then  'x' ELSE '' END) as 'PC',
MAX(CASE WHEN gp.id_platform = 2 then  'x' ELSE '' END) as 'MAC',
MAX(CASE WHEN gp.id_platform = 3 then  'x' ELSE '' END) as 'X360',
MAX(CASE WHEN gp.id_platform = 4 then  'x' ELSE '' END) as 'PS3',
MAX(CASE WHEN gp.id_platform = 5 then  'x' ELSE '' END) as 'XONE',
MAX(CASE WHEN gp.id_platform = 6 then  'x' ELSE '' END) 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2016-04-16
    • 2010-09-07
    • 2018-02-20
    • 1970-01-01
    • 2017-12-11
    • 2023-04-07
    相关资源
    最近更新 更多