2012-11-11 107 views
0
SELECT * 
FROM banhammer_bans 
INNER JOIN banhammer_players AS player 
    ON banhammer_bans.player_id = player.id 
INNER JOIN banhammer_players AS admin 
    ON banhammer_bans.creator_id = admin.id 
ORDER BY created_at DESC 

單獨列名後,我得到三個id列,並從banhammer_players表中的兩列name。我如何分開這些,以便我可以在我的PHP代碼中使用它們?SQL:兩個內連接

banhammer_players包含兩列,idname。 banhammer_bans有兩列(player_idcreator_id),它們都與banhammer_players表相匹配。

+1

在SELECT語句中閱讀關於別名和使用表名稱 –

回答

1

使用別名顯式選擇所有列。

SELECT banhammer_bans.id as bans_id, banhammer_players as players_id, ... 
FROM banhammer_bans 
INNER JOIN banhammer_players AS player 
    ON banhammer_bans.player_id = player.id 
INNER JOIN banhammer_players AS admin 
    ON banhammer_bans.creator_id = admin.id 
ORDER BY created_at DESC 
+0

謝謝,顯然我覺得有點太累了,以至於我實際上已經擁有了播放器。*和admin。*別名。 – dataviruset

0

使用別名查詢爲:

SELECT bans.id, player.name AS player_name, admin.name AS admin_name 
    FROM banhammer_bans AS nans 
    INNER JOIN banhammer_players AS player 
    ON bans.player_id = player.id 
    INNER JOIN banhammer_players AS admin 
    ON bans.creator_id = admin.id 
    ORDER BY created_at DESC; 

在一個以上的使用模棱兩可列的表的別名(相同的名字列在select屬性你希望你可以添加儘可能多的表)。