MySQL - 连接表时出现错误#1055

时间:2017-06-19 10:12:39

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

我有以下表格:

  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. game_platform

    +----+---------+-------------+
    | 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 |
    +----+---------+-------------+
    
  4. 结果我想要这样的东西:

        +-----------------------------------+----+-----+------+-----+------+-----+
        | 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的情况下对表中的数据进行分组? 我已经知道这个选项意味着什么以及为什么它被引入。我正在使用的数据库不是自托管数据库,所以无论如何我都无法将其关闭。

1 个答案:

答案 0 :(得分: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