简体   繁体   中英

Join tables when 3 column in first table that can point to same column in second table

I have the following DB structure:

在此处输入图像描述

And right now I can't make up a query to get a creator data, admin data and tech data from item_contacts...

What kind of JOIN I need to use and how?

I think you want 3 joins on item_contacts - one for each column whose data you want to recover:

select
    i.*,
    cc.data as creator_data,
    ca.data as admin_data,
    ct.data as tech_data
from items i
inner join item_contacts cc on cc.contact_id = i.creator_id
inner join item_contacts ca on ca.contact_id = i.admin_id
inner join item_contacts ct on ct.contact_id = i.tech_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM