从相同的LEFT JOIN表中检索两次匹配数据?

时间:2013-10-27 21:46:53

标签: mysql left-join

信息

有没有办法可以在一个LEFT JOIN中两次从另一个(同一个)表中检索两位信息?

尝试

下面是我试图放在一起的SQL查询,希望能有效。我希望你能看到我想从中做些什么。

SELECT cards.*, list.name FROM cards LEFT JOIN list ON cards.main = list.id AS main_name AND cards.enemy = list.id AS enemy_name WHERE cards.id = 1

如上所示,我正在尝试从cards.main表中检索两个值cards.enemylist的名称。

谢谢,任何问题都会尽快回答!

1 个答案:

答案 0 :(得分:0)

您必须两次加入列表表:

SELECT cards.*, main_name.name, enemy_name.name 
FROM cards 
LEFT JOIN list AS main_name ON cards.main = main_name.id
LEFT JOIN list AS enemy_name ON cards.enemy = enemy_name.id
WHERE cards.id = 1