我正在尝试进行简单的查询,但无法获得所需的结果。
我有一个带有两个引用ID的表BATTLE。我想要的是在一行中,每个英雄的名字基于其他表HERO
我在做这个查询
SELECT
battle.character1_id as p1,
character.name,
battle.character2_id as p2,
character.name
FROM
battle,
character
WHERE
character.id in (character1_id, character2_id)
但我在战斗中得到两排。我知道我做错了什么但我不知道是什么?
答案 0 :(得分:2)
您需要在battle
表格中加入hero
两次,每次一次:
SELECT h1.id AS id1, h1.name AS name1, h2.id AS id2, h2.name AS name2
FROM battle b
JOIN hero h1 ON h1.id = b.hero1_id
JOIN hero h2 ON h2.id = b.hero2_id