表格设置如下:
fighters:
fighterid
firstname
lastname
etc
etc
matches:
matchid
fighteroneid (fighterid FK)
fightertwoid (fighterid FK)
etc
etc
我想将战斗机和战斗机加入到战斗机中,它们具有相同的matchid(我很确定我已经弄清楚了这部分),但我失败的地方是尝试将其显示在列表框如下:
firstname lastname vs firstname lastname
例如,我可以使用 COALESCE 将fighteroneid 和fightertwoid 显示为1 vs 2,但是如何在给定这些ID 的情况下链接到每个战斗机的名字和姓氏?
提前致谢,
斯宾塞
最佳答案
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
关于postgresql - 连接两个表、两列,同时将多行显示为一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12985162/