question

billybox avatar image
billybox asked

connect primary and foreign key using left join

basic #parenttable# bcity #row# Foreign keys source target bcityId cityID Hey guys, i would like to link BASIC (parent table) with the foreign key, but cant figure it how .. theres also a primary ID for BASIC .. im trying to pull out the CITY NAME using LEFT JOIN tried this, but doe not retrieve the data .. :/ Select basic.city from basic left join city on bcityId = city.ID could someone help ? thanks
primary-keyforeign-keyleft join
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

billybox avatar image billybox commented ·
thanks a lot, well .. for example i have 3 tables 1. religion 2.religiontype 3. profilebasic Religion has 3 rows ID profileID religionTypeID Religiontype has 2 rows ID religionTypeName profilebasic has 4 rows ID firstName middleName lastName Profilebasic is the parenttable and religiontype.ID is tied to religion.religionTypeID and religion.profileID is tied to profilebasic.ID how can i write this down with left joins ?
0 Likes 0 ·
Grant Fritchey avatar image Grant Fritchey ♦♦ commented ·
This web site runs off of votes. Can you please indicate all helpful answers below by clicking on the thumbs up next to those answers. If any one answer lead to a solution, please indicate that by clicking on the check mark next to that one answer.
0 Likes 0 ·
iainrobertson avatar image
iainrobertson answered
You have your join expression the wrong way round. You're retrieving all rows from the basic table, regardless of match in bcity. If you swap then over, it should get you what you need: select b.city from city c left join basic b on c.ID = b.BCityId
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

iainrobertson avatar image iainrobertson commented ·
Hmm. I suspect that I might be on completely the wrong track here. What are you actually trying to do? What set of results do you expect?
0 Likes 0 ·
Tom Staab avatar image
Tom Staab answered
Am I correct that you have a 1 to many relationship between ReligionType and ProfileBasic? I believe this is what you are looking for: SELECT pb.*, rt.religionTypeName FROM Profilebasic pb LEFT JOIN Religion r ON r.profileID = pb.ID LEFT JOIN ReligionType rt ON rt.ID = r.religionTypeID ; If I understand your structure correctly, that will return every profile along with the religion type if found.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.