简体   繁体   English

2多对多关系-通过联接提取数据

[英]2 Many to Many relationship - extract data with join

Please check the schema in the link below. 请检查以下链接中的架构。

http://s11.postimg.org/dydr5ru3n/stack.png 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
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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