表格设置如下:
fighters:
fighterid
firstname
lastname
etc
etc
matches:
matchid
fighteroneid (fighterid FK)
fightertwoid (fighterid FK)
etc
etc
我想加入fighteroneid和fightertwoid到fighterid他们有相同的matchid(我很确定我已经找到了这个部分),但我失败的地方是试图显示它列表框如下:
firstname lastname vs firstname lastname
例如,我可以使用COALESCE显示fighteroneid和fightertwoid为1 vs 2,但是如何根据这些ID链接到每个战斗机的名字和姓氏?
提前致谢,
彭斯
答案 0 :(得分:2)
SELECT
f1.firstname
, f1.lastname
, f2.firstname
, f2.lastname
FROM matches AS m
INNER JOIN fighters AS f1
ON f1.fighterid = m.fighteroneid
INNER JOIN fighters AS f2
ON f2.fighterid = m.fightertwoid