nasia jaffri
nasia jaffri

Reputation: 823

Outer Join with 3 tables using SQL

I am trying to do an outer join on three tables.

Select longitue, latitude, name from
UserEntry
Geo
TwTbl
where UserEntry.id = TwTbl.user_id
and Geo.id = TwTbl.Geo_count

but this resulting in only what is common in all 3 tables. I need to do an outer join, but do not know the syntax while using 3 tables. I need to keep everything in the UserEntry table. Can someone please help me.

Upvotes: 1

Views: 109

Answers (2)

M.Ali
M.Ali

Reputation: 69524

Left Join

Select longitue, latitude, name 
from UserEntry LEFT JOIN TwTbl
ON  UserEntry.id =  TwTbl.user_id
LEFT JOIN  Geo
ON Geo.id = TwTbl.Geo_count

This will give you all the rows from UserEntry and matching rows between the UserEntry , Then joining that result set to TwTbl and giving you all the rows from the result set of 1st two tables join and the matching rows from TwTbl.
Full Join

Select longitue, latitude, name 
from UserEntry FULL JOIN TwTbl
ON  UserEntry.id =  TwTbl.user_id
FULL JOIN  Geo
ON Geo.id = TwTbl.Geo_count

This kind of join will give you all the rows from all three tables matching and non-matching rows.
Right Join

Select longitue, latitude, name 
from UserEntry RIGHT JOIN TwTbl
ON  UserEntry.id =  TwTbl.user_id
RIGHT JOIN  Geo
ON Geo.id = TwTbl.Geo_count

This will give you matching rows from UserEntry Table and all the rows from Geo Table and then joining this result set to TwTbl will give you all rows from result set of 1st join and all the rows from TwTbl

Upvotes: 2

Wilton
Wilton

Reputation: 226

I think that you are looking for a full outer join. Something like this:

SELECT longitue, latitude, name from
UserEntry
FULL OUTER JOIN TwTbl
ON TwTbl.user_id = UserEntry.id
FULL OUTER JOIN Geo
ON Geo.id = twtbl.geo_count

Upvotes: 1

Related Questions