1

我有以下表格:

companyContacts
addresses
states
phoneNumbers

我需要在 、 和 上进行addressesLEFT statesJOIN phoneNumbers。我需要与 连接,phoneNumbers并且需要与连接。addressescompanyContactsstatesaddresses

我可以进行如下选择来获取所有内容。但是,如果某人有地址而不是电话号码,则不会返回结果。我如何才能使两者phoneNumbersaddresses从 加入companyContacts

SELECT * FROM
    companyContacts AS c
    LEFT JOIN phoneNumbers AS p
        ON c.entityID = p.entityID
    LEFT JOIN addresses AS a
        ON p.entityID = a.entityID
    LEFT JOIN states AS s
        ON a.stateID = s.id
4

1 回答 1

3

听起来你想要这个:

SELECT * 
FROM  companyContacts AS c
LEFT JOIN phoneNumbers AS p
   ON c.entityID = p.entityID
LEFT JOIN addresses AS a
   ON c.entityID = a.entityID
LEFT JOIN states AS s
   ON a.stateID = s.id

如果您同时加入companyContacts.entityId两者phoneNumbersaddresses那么即使没有电话号码或地址,您也会返回记录。你写电话号码的方式需要存在才能返回地址

于 2012-12-20T18:07:41.117 回答