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

时间:2015-05-05 23:37:56

标签: mysql sql join

请检查以下链接中的架构。

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

我想选择所有属性以及每个属性的代理和所有者。问题是,代理商和所有者的详细信息都来自同一个表,并且存在冲突。

我应该遵循什么样的解决方案?

注意:遗憾的是我无法更改数据库架构。

1 个答案:

答案 0 :(得分:0)

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
;