简体   繁体   中英

2 Many to Many relationship - extract data with join

Please check the schema in the link below.

http://s11.postimg.org/dydr5ru3n/stack.png

I want to select all properties and also the agent and the owner of each property. The problem is that both agent's and owner's details comes from the same table and there is a conflict.

What solution should i follow?

Note: Unfortunately I cannot change the db schema.

SELECT oPers.first_name AS ownerFirstName
    , aPers.first_name AS agentFirstName
    ....
FROM properties AS p
LEFT JOIN owners AS po ON p.property_id = po.property_id
LEFT JOIN people AS oPers ON po.person_id = oPers.person_id
LEFT JOIN agents AS pa ON p.property_id = pa.property_id
LEFT JOIN people AS aPers ON pa.person_id = aPers.person_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