misguided
misguided

Reputation: 3799

How to make a table left outer join of multiple tables

I am need to join a main table with two feed tables.Table data below:

MainTable

enter image description here

FeedTable

enter image description here

Query I am running is left inner join

Select ID, MT.ColumnA, FT1.ColumnA, FT1.ColumnB, FT2.ColumnA, FT2.ColumnC, FT3.ColumnA, FT3.ColumnD
from MainTable MT
Left Join FeedTable FTI1 on FT1.fk1 = MT.key
Left Join FeedTable FT2 on FT2.fk2 = MT.key
Left Join FeedTable FT3 on FT3.fk3 = MT.key

The output I get is :

enter image description here

The Output I want to get is :

enter image description here

I am assuming left outer join isn't the way to go about this or am I doing the join wrong?

Upvotes: 0

Views: 21

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270793

You want the tables in the wrong order. The FeedTable should be first:

Select ft.*, mt1.key as cola, mt2.key as colb, mt3.key colc
from FeedTable ft left join
     MainTable mt1
     on ft.fk1 = mt1.key left join
     MainTable mt2
     on ft.fk2 = mt2.key left join
     MainTable mt2
     on ft.fk3 = mt3.key;

Upvotes: 1

Related Questions