在SQL中连接3个表并显示每个表中的列

时间:2018-06-04 10:32:15

标签: sql join

我有三个表共享一个公共ID,我希望通过基于这个公共ID加入来显示每个表中的列。

我可以加入三个表中的两个,但是当我包含第三个表时,我会收到错误。

这是我的剧本:

select TableA.CommonID, TableA.Column1, TableB.Column2, TableC.Column3
from TableA
join TableB on TableA.CommonID = TableB.CommonID
join TableC on TableA.CommonID = TableC.CommonID;

我做错了什么?

2 个答案:

答案 0 :(得分:0)

尝试使用InnerJoin来满足您的要求

SELECT a.CommonID, a.Column1, b.Column2, c.Column3
FROM TableA AS a
INNER JOIN TableB AS b ON a.CommonID = b.CommonID
INNER JOIN TableC AS c ON a.CommonID = c.CommonID

答案 1 :(得分:0)

使用MySQL我没有错误。 请参阅我的测试here 根据评论,有必要提供有关DBMS和错误的更多详细信息。

相关问题