Inner joining multiple columns to one ID [duplicate]
Inner joining multiple columns to one ID [duplicate]
This question already has an answer here:
I'm not in charge of the database so I can't change the format, I realize it is a horrible database.
I have two tables I want to join:
Table1: address_book
id | name | address | phone number | email
Table2: team
id | person1_id | person2_id | person3_id | person4_id | person5_id | person6_id
I would like to join all the personX_id with the name from address_book. I can't seem to figure out how to join more than one column. Hoping someone here could help!
Thanks
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1 Answer
1
if you have only 6 colums for person_id you could use a multiple join
select a1.name
from team t
left join address_book a1 on a1.name = t.person1_id
left join address_book a2 on a2.name = t.person2_id
left join address_book a3 on a3.name = t.person3_id
left join address_book a4 on a4.name = t.person4_id
left join address_book a5 on a5.name = t.person5_id
left join address_book a6 on a6.name = t.person6_id