この問題に関連する 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などを試しましたが、うまくいきません。助けてください!
事前にご協力いただきありがとうございます。