我正在尝试显示来自3个表格的数据(我没有创建自己的方案)。 表格具有以下简化属性:
table1: messageID, fromID
table2: messageID, toID
table3: id, displayName
table3中的id对应于fromID和toID。
我想显示messageID的视图,fromDisplayName,toDisplayName。
到目前为止我所拥有的是:
SELECT table1.messageID as id, displayName as fromDisplayName, displayName as toDisplayName
FROM table1
INNER JOIN table2 ON table1.messageID = table2.messageID
INNER JOIN table3 ON table1.fromID = table3.id AND table2.toID = table3.id
显然,displayName两次都是同一个。如何显示两个不同的displayNames?
答案 0 :(得分:2)
SELECT t1.messageID,
fromName.displayName as fromDisplayName,
toName.displayName as toDisplayName
FROM table1 t1
JOIN table2 t2
ON t1.messageID = t2.messageID
JOIN table3 fromName
ON t1.fromID = fromName.id
JOIN table3 toName
ON t2.toID = toName.id