简体   繁体   中英

Joining 3 tables in SQL and displaying columns from each

I have three tables that all share a common ID and I wish to display columns from each of these tables by joining based on this common ID.

I am able to join two of the three tables but when I include the third I get errors.

This is my script:

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;

What Am I doing wrong?

Try use InnerJoin to satisfy your requirement

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

I have no error by using MySQL. Please see my test here As per comment it is necessary more details on DBMS and error you get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM