SQL JOIN查询不起作用

时间:2013-10-01 16:28:09

标签: mysql sql join left-join inner-join

此问题涉及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等,但没有运气。请帮忙!

提前感谢您的帮助。

4 个答案:

答案 0 :(得分:0)

我注意到你在加入时加入了contact_id = id,但是在你的工作查询中你检查了contact_id是否接触过。更改您的联接以使用您在工作查询中使用的相同列。

要进行调试,请尝试以下方法:

SELECT  *
FROM    contacts c1            
WHERE   c1.country LIKE '%Korea, North%' 

并查看它返回的内容。然后一直添加一个表,直到你得到0行。

SELECT  *
FROM    contacts c1            
INNER JOIN contact_to_categories c2 
            ON c2.contactid = c1.id
WHERE   c1.country LIKE '%Korea, North%' 

接下来,如果有效,请在where子句中添加:

c2.catid = 2

等等。

答案 1 :(得分:0)

您的WHERE子句可能过滤掉了太多的值。尝试将条件放入连接中。

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
        AND c2.catid = 2
    JOIN `contact_address` c4 ON c4.`contact_id` = c1.`id` 
        AND c4.hmcountry like '%Korea, North%'
    JOIN `contact_offices` c5 ON c5.`contact_id` = c1.`id`
        AND c5.off_country like '%Korea, North%'

WHERE c1.country like '%Korea, North%'

此外,任何NULL都可能导致行丢失。我会尝试向左或向右连接,以查看连接链中丢失行的位置。另外,打破这一点,一次加入一个。从基表开始。添加一个联接,看它是否返回预期结果,然后添加下一个,依此类推。

如果有帮助,请告诉我。

答案 2 :(得分:0)

这里有几种可能性。

第一个可能说明显而易见,我怀疑是这样,但在你的第一个问题中你有:

WHERE   c2.catid = 2 

在接下来的两个查询中,您有:

WHERE `catid` IN (2, 3, 6)

这两个带回的所有行都可能在CatID 3和6中吗?这可以解释为什么您的第一个查询没有返回任何结果。

第二个概率更高(假设您使用WHERE CatID = 2时第2和第3个查询仍会返回记录) 是没有联系人在朝鲜有家庭住址,也有办公地址,或在朝鲜有办公地址,也有家庭住址。

我不确定您使用LEFT JOIN尝试了什么,但我不明白为什么如果您的小型查询都这样做,这不会返回结果。

SELECT  c1.id, 
        title, 
        fname, 
        lname, 
        c1.country as country, 
        c4.hmcountry as hmcountry, 
        c5.off_country as off_country 
FROM    contacts
        INNER JOIN contact_to_categories c2 
            ON c2.contactid = c1.id
        LEFT JOIN `contact_address` c4 
            ON c4.`contact_id` = c1.`id` 
        LEFT 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%' 
    );

答案 3 :(得分:0)

我已经对与N. Korea相关联的任何一个联系人,家庭住址或办公室地址联合的所有不同联系人ID进行了预查询。从中,加入到类别的联系人以过滤掉您的2,3,6类别。所以现在你只有一个符合其身份资格的联系人列表。

现在,使用该列表连接回那些合格ID的主要联系人列表,并通过LEFT-JOIN加入各自的家庭或办公室地址位置,以防两者中只有一个存在或者只存在其中一个。如果是地址?很好,抓住它,否则显示为空白

SELECT
      PQ.Contact_ID,
      cm2.title,
      cm2.fname,
      cm2.lname,
      cm2.country,
      coalesce( ha2.hmcountry, ' ' ) as HomeCountry,
      coalesce( oa2.off_country, ' ' ) as OfficeCountry
   from 
      ( select distinct cm.id as Contact_ID
           from contact_master cm
           where cm.country like '%Korea, North%' 
        union
        select distinct ha.contact_id
           from home_address ha
           where ha.hmcountry like '%Korea, North%' 
        union
        select distinct oa.contact_id
           from office_address oa
           where oa.off_country like '%Korea, North%' ) as PQ

      JOIN contact_to_categories c2c
         on PQ.Contact_ID = c2c.contactid
         AND c2c.catid in(2,3,6)

      JOIN contact_master cm2
         ON PQ.Contact_ID = cm2.id
         LEFT JOIN home_address ha2
            ON PQ.Contact_ID = ha2.Contact_ID
         LEFT JOIN office_address oa2
            ON PQ.Contact_ID = ha2.Contact_ID