在MYSQL中连接多个表时出错(

时间:2016-06-16 09:37:04

标签: mysql sql

我有三张桌子

phoneRecord - ID,phone
companies - ID,phone1,phone2, name
contacts - ID, phone1, phone2, companies.ID

我希望将表格电话记录加入公司和联系人,我可以这样做,具体如下:

SELECT 
    phoneRecord.*, 
    companies.ID, 
    contacts.ID, 
FROM phoneRecord
    LEFT JOIN contacts
    ON phoneRecord.Phone=contacts.Phone1 OR phoneRecord.Phone=contacts.Phone2
    LEFT JOIN companies ON phoneRecord.Phone=companies.phone1 OR phoneRecord.phone=companies.phone2 

但是,我还需要一件事。当联系人返回时,我想知道联系人所在的公司(如果contact.companies匹配company.id)。有可能这样做吗?

我尝试过类似的事情:

 SELECT 
    phoneRecord.*, 
    companies.ID, 
    companies.name, 
    contacts.ID, 
FROM phoneRecord
    LEFT JOIN contacts
        LEFT JOIN companies ON contacts.company=companies.ID
    ON phoneRecord.Phone=contacts.Phone1 OR phoneRecord.Phone=contacts.Phone2
    LEFT JOIN companies ON phoneRecord.Phone=companies.phone1 OR phoneRecord.phone=companies.phone2 

但它给我一个错误代码1066:不是唯一的表/别名:'公司'。

我已经像托马斯建议的那样切换了代码,但它给了我同样的错误。也许我误解了他的意思。代码是这样的:

SELECT
    phoneRecord.*, 
    companies.ID, 
    companies.name, 
    contacts.ID, 
FROM phoneRecord
    LEFT JOIN contacts ON phoneRecord.Phone=contacts.Phone1 OR phoneRecord.Phone=contacts.Phone2
    LEFT JOIN companies ON contacts.company=companies.ID
    LEFT JOIN companies ON phoneRecord.Phone=companies.phone1 OR phoneRecord.phone=companies.phone2 

我终于用它来了解它:

SELECT 
    phoneRecordID,
    phoneRecordPhone,
    companyID,
    contactsID,
    contactsCompany,
    companies.name
    FROM(
SELECT 
    phoneRecord.ID as phoneRecordID, 
    phoneRecord.Phone as phoneRecordPhone,
    companies.ID as companyID, 
    contacts.ID as contactsID, 
    contacts.company as contactsCompany
FROM phoneRecord
    LEFT JOIN contacts
    ON phoneRecord.Phone=contacts.Phone1 OR    phoneRecord.Phone=contacts.Phone2
    LEFT JOIN companies ON phoneRecord.Phone=companies.phone1 OR phoneRecord.phone=companies.phone2) as result
LEFT JOIN companies ON contactsCompany = companies.id

谢谢,这非常有用。

3 个答案:

答案 0 :(得分:0)

我在结果中检索列表然后使用左联接与联系,希望这将适合您

   SELECT cmid from( SELECT 
   phoneRecord.*, 
   companies.ID as cmid, 
  contacts.ID, 
  FROM phoneRecord
  LEFT JOIN contacts
  ON phoneRecord.Phone=contacts.Phone1 OR phoneRecord.Phone=contacts.Phone2
   LEFT JOIN companies ON phoneRecord.Phone=companies.phone1 OR phoneRecord.phone=companies.phone2) as result 
    left join contacts on contacts.ID = result.cmid

答案 1 :(得分:0)

这是另一种方法 -

 WITH CompanyInfo As(
   SELECT 
   phoneRecord.ID as phoneRecordID, 
   phoneRecord.Phone as phoneRecordPhone,
   companies.ID as companyID, 
   contacts.ID as contactsID, 
   contacts.company as contactsCompany
   FROM phoneRecord
   LEFT JOIN contacts
   ON phoneRecord.Phone=contacts.Phone1 OR phoneRecord.Phone=contacts.Phone2
   LEFT JOIN companies ON phoneRecord.Phone=companies.phone1 OR phoneRecord.phone=companies.phone2
   )

  SElECT * FROM CompanyInfo 
  LEFT JOIN companies ON CompanyInfo.contactsCompany = companies.id

答案 2 :(得分:0)

DBMS抱怨,因为您加入companies两次,并且在您使用companies.phone1时,它不知道您正在谈论的两个记录。您必须提供两个不同的别名,以便区分它们。 E.g:

left join companies com on p.phone in (com.phone1, com.phone2)
left join contacts con on p.phone in (con.phone1, con.phone2)
left join companies concom on con.company = concom.id

然而,对我来说,似乎你选择了两件不同的东西:

  1. 联系人(公司)的电话记录
  2. 公司的电话记录
  3. 所以可能想要两个查询结果:

    select p.*, com.id as company_id, com.name, con.id as contact_id
    from phonerecord p
    join contacts con on p.phone in (con.phone1, con.phone2)
    join companies com on con.company = com.id
    union all
    select p.*, com.id as company_id, com.name, null  as contact_id
    from phonerecord p
    join companies com on p.phone in (com.phone1, com.phone2);