Link to home
Start Free TrialLog in
Avatar of cbrune
cbrune

asked on

How to Join multiple columns that reference themselves

I have table A{
id,
name
}

table B{
id,
aid,
bid,
cid
}

I need to go left joins on Table B to table A.. and show all table B and the references values for each row.
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vitor Montalvão
Why do you need a Left Join?
From what you write you want to start in table B and left join table A, it's very basically
Select B.*, A.name from B left join A on B.aid = A.id

Open in new window

Or am I missing anything in the meanings of aid,bid and cid in table B?

Bye, Olaf.
But let's say table A is persons and table B is threads aid, bid and cid are all person IDs, eg

aid = QuestionByPersonID
bid = AnswerByPersonID

then you do two left joins:
Select B.*, 
aa.name as Asking,
ab.name as Answering 
From B 
Left Join A as aa on aa.id = B.aid
Left Join A as ab on ab.id = B.bid

Open in new window

Bye, Olaf.