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

我从 banhammer_players 表中得到三id列和两列。name如何将它们分开,以便在我的 PHP 代码中使用它们?

banhammer_players 包含两列,idname. banhammer_bans 有两列 (player_idcreator_id) 都与 banhammer_players 表匹配。

4

2 回答 2

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
于 2012-11-11T20:41:51.450 回答
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为不明确的列(多个表中的同名列)使用表别名时添加任意数量的属性。

于 2012-11-11T20:42:08.043 回答