此问题涉及 5 个表:
[contact master]
id (pk)
title
fname
lname
country
[home address]
id (pk)
contact_id (fk)
hmcountry
...
[office address]
id (pk)
contact_id (fk)
off_country
...
[category master]
id (pk)
name
[category to contacts]
id (pk)
catid
contactid
[以下查询返回 0 行]
select
c1.id,
title,
fname,
lname,
c1.country as country,
c4.hmcountry as hmcountry,
c5.off_country as off_country
from
contacts
join contact_to_categories c2 on c2.contactid=c1.id
join `contact_address` c4 ON c4.`contact_id` = c1.`id`
join `contact_offices` c5 ON c5.`contact_id` = c1.`id`
where
c2.catid=2
and ( c1.country like '%Korea, North%'
or c4.hmcountry like '%Korea, North%'
or c5.off_country like '%Korea, North%' )
[以下工作正常并返回预期结果]
SELECT
`contact_id`
FROM
`contact_address`
WHERE
`hmcountry` like '%Korea, North%'
and `contact_id` in (select `contactid`
from `contact_to_categories`
where `catid` in(2,3,6) )
[甚至这也有效]
SELECT
`contact_id`
FROM
`contact_offices`
WHERE
`off_country` like '%Korea, North%'
and `contact_id` in ( select `contactid`
from `contact_to_categories`
where `catid` in(2,3,6) )
有什么建议我做错了吗?
我要做的是在主要联系人主表或办公室地址或家庭地址中找到所有以“韩国,北方”为国家的联系人。我尝试过 INNER JOIN、LEFT JOIN 等,但没有运气。请帮忙!
提前感谢您的帮助。