2017-06-19 37 views
2

時,我有以下表格:MySQL的 - 錯誤#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 | 
    +----+---------+-------------+ 
    

而作爲一個結果,我想是這樣的:

+-----------------------------------+----+-----+------+-----+------+-----+ 
    | 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? 我已經知道這個選項的含義以及它爲什麼被引入。我使用的數據庫不是自己託管的數據庫,因此無論如何我都無法將其關閉。

+0

這就是所謂的透視表和這個問題已經被問,並回答了在這裏SO SOOO很多次。鏈接的重複主題描述了MySQL中的靜態和動態主題。但是,請注意,在應用程序邏輯中執行此類轉換可能比在SQL中更有效。 – Shadow

回答

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