我要做的是进行查询,以便我可以使战斗表更具可读性 因此战斗机1会从战斗机表中拉出1的id,而战斗机2会做同样的事情。获胜者是0,1,2。零意味着它是平局,然后1表示战斗机1获胜,2表示战斗机2获胜。
到目前为止我尝试的查询:
SELECT CONCAT(first_name, ' ', last_name) 'Fighter 1',
CONCAT(victory_method.name, ' (', method_notes, ')') 'Victory'
FROM fights
JOIN fighters ON fights.fighter1_id = fighters.id
JOIN victory_method ON fights.victory_method_id = victory_method.id
现在这是一个好的开始,但我需要的是弄清楚如何在那里列出战斗机2,然后也是胜利者,无论是抽奖还是战斗机1或2谁赢了。 / p>
答案 0 :(得分:2)
as
再次内部加入战斗机表
SELECT
CONCAT(fo.first_name, ' ', fo.last_name, if (fights.winner=1, "Winner", "Loser")) 'Fighter 1',
CONCAT(ft.first_name, ' ', ft.last_name, if (fights.winner=2, "Winner", "Loser")) 'Fighter 2',
CONCAT(victory_method.name, ' (', method_notes, ')') 'Victory'
FROM fights
INNER JOIN fighters as fo ON fights.fighter1_id = fo.id
INNER JOIN fighters as ft ON fights.fighter2_id = ft.id
INNER JOIN victory_method ON fights.victory_method_id = victory_method.id