I have the following tables:
companyContacts
addresses
states
phoneNumbers
And I need to do a LEFT JOIN on addresses
, states
, and phoneNumbers
. I need to connect phoneNumbers
and addresses
with companyContacts
, and need to connect states
with addresses
.
I can do a select like the following to grab everything. However, If someone has an address and not a phone number, it will not return a result. How do I make it so that both phoneNumbers
and addresses
are joined from 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
It sounds like you want this:
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
If you join on the companyContacts.entityId
to both the phoneNumbers
and addresses
then you will return records even if there is not a phone number or address. The way you had it written the phone number needed to exist to return an address
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.