1

我正在尝试检索联系人所属公司的名称。关系存在于表中account_contacts,但是当我尝试调整它的查询时

SELECT
    accounts.`name`,
    contacts.first_name
FROM
    contacts,
    accounts
INNER JOIN accounts_contacts ON contacts.id = accounts_contacts.contact_id
AND accounts.id = accounts_contacts.account_id

我得到的错误是

[Err] 1054 - Unknown column 'contacts.id' in 'on clause'

更改后:

SELECT
accounts.`name`,
contacts.first_name,
accounts.id
FROM
    contacts
INNER JOIN accounts_contacts ON contacts.id = accounts_contacts.contact_id
JOIN accounts ON accounts.id = accounts_contacts.account_id
WHERE first_name = 'shamraiz' 

您的查询返回 2 行,结果符合我的预期。账户ID不同。但是,我重做的查询以您的方式再次执行它不起作用。accountid 相同,但返回 2 行。

SELECT
    contacts.id AS CONTACTID,
    accounts.id AS ACCOUNTID,
    contacts.first_name,
    contacts.last_name,
    contacts.phone_work,
    contacts.phone_fax,
    contacts.department,
    contacts.title,
    contacts.description,
    contacts.salutation,
    email_addresses.email_address,
    contacts.deleted
FROM
    contacts
INNER JOIN accounts_contacts ON contacts.id = accounts_contacts.contact_id
    JOIN accounts on accounts.id = accounts_contacts.account_id
INNER JOIN email_addr_bean_rel ON contacts.id = email_addr_bean_rel.bean_id
INNER JOIN email_addresses ON email_addresses.id = email_addr_bean_rel.email_address_id

where first_name = 'shamraiz'

下一个查询返回 3 行,但前 2 行重复

SELECT
    contacts.id AS CONTACTID,
    accounts.id AS ACCOUNTID,
    contacts.first_name,
    contacts.last_name,
    contacts.phone_work,
    contacts.phone_fax,
    contacts.department,
    contacts.title,
    contacts.description,
    contacts.salutation,
    email_addresses.email_address,
    contacts.deleted
FROM
    contacts
inner JOIN accounts_contacts ON contacts.id = accounts_contacts.contact_id
    left JOIN accounts on accounts.id = accounts_contacts.account_id
left JOIN email_addr_bean_rel ON contacts.id = email_addr_bean_rel.bean_id
left JOIN email_addresses ON email_addresses.id = email_addr_bean_rel.email_address_id
where first_name = 'shamraiz'

来自联系人

SELECT * FROMSugarcrm.联系人where first_name = 'shamraiz'返回 2 行

来自 account_contact 关系

SELECT * FROMsugarcrm .accounts_contactswhere contact_id = '17619b5e-db07-fa3b-6748-51a73ef38c5e'返回 1 行

SELECT * FROMSugarcrm .accounts_contactswhere contact_id = '003b0000006ZMDXAA4'返回 1 行。

因此,最终查询应返回 2 个不同的行,因为它们是两个姓名相似的联系人加入了 2 个不同的公司。

一个联系人可以属于 1 个公司。

更多调整:

我做了一些修改,但它返回了 1 条记录。应该返回 2. 我需要它来提取电子邮件地址是否存在关系的记录。

SELECT
    contacts.id AS CONTACTID,
    accounts.id AS ACCOUNTID,
    contacts.first_name,
    contacts.last_name,
    contacts.phone_work,
    contacts.phone_fax,
    contacts.department,
    contacts.title,
    contacts.description,
    contacts.salutation,
    EM.email_address,
    contacts.deleted,
    EABR.primary_address
FROM
    contacts
LEFT JOIN accounts_contacts ON contacts.id = accounts_contacts.contact_id
JOIN accounts ON accounts.id = accounts_contacts.account_id
LEFT JOIN email_addr_bean_rel EABR ON contacts.id = EABR.bean_id
AND (
    EABR.primary_address = 1
    || (EABR.primary_address IS NOT NULL AND EABR.primary_address != 0)
) 
JOIN email_addresses EM ON EABR.email_address_id = EM.id
WHERE
    contacts.first_name = 'shamraiz'

已解决的答案:

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`%` SQL SECURITY DEFINER VIEW `view_contacts_sugar_hdb`
AS
select
    `hdb`.`contacts`.`CONTACTID` AS `CONTACTID`,
    `hdb`.`contacts`.`CLIENTID` AS `CLIENTID`,
     concat(`hdb`.`contacts`.`FIRSTNAME`,_utf8' ',coalesce(`hdb`.`contacts`.`INITIALS`,_utf8'')) AS `FIRSTNAME`,
    `hdb`.`contacts`.`LASTNAME` AS `LASTNAME`,
    `hdb`.`contacts`.`PHONE` AS `PHONE`,
    `hdb`.`contacts`.`FAX` AS `FAX`,
    `hdb`.`contacts`.`DEPARTMENT` AS `DEPARTMENT`,
    `hdb`.`contacts`.`TITLE` AS `TITLE`,
    `hdb`.`contacts`.`INFO` AS `INFO`,
    `hdb`.`contacts`.`SALUTATION` AS `SALUTATION`,
    `hdb`.`contacts`.`EMAIL` AS `EMAIL`,
    CASE
        WHEN `hdb`.`contacts`.`ACTIVE` != 0 THEN 0
        ELSE 1
    END DELETED,
    'paradox' AS `SOURCEDATABASE`
    from `hdb`.`contacts`
    union
        SELECT
        contacts.id AS CONTACTID,
        accounts_contacts.account_id AS CLIENTID,
        contacts.first_name AS FIRSTNAME,
        contacts.last_name AS LASTNAME,
        contacts.phone_work AS PHONE,
        contacts.phone_fax AS FAX,
        contacts.department AS DEPARTMENT,
        contacts.title AS TITLE,
        contacts.description AS INFO,
        contacts.salutation AS SALUTATION,
        email_addresses.email_address AS EMAIL,
        contacts.deleted AS DELETED,
        'sugar' AS SOURCEDATABASE
    FROM
        (
            (
                (
                    sugarcrm.contacts
                    LEFT JOIN sugarcrm.email_addr_bean_rel ON (
                        (
                            contacts.id = email_addr_bean_rel.bean_id
                        )
                    )
                    AND (
                        email_addr_bean_rel.primary_address = 1 || (
                            email_addr_bean_rel.primary_address IS NOT NULL
                            AND email_addr_bean_rel.primary_address != 0
                        )
                    )
                )
                LEFT JOIN sugarcrm.accounts_contacts ON (
                    (
                        contacts.id = accounts_contacts.contact_id
                    )
                )
            )
            JOIN sugarcrm.email_addresses ON (
                (
                    email_addr_bean_rel.email_address_id = email_addresses.id
                )
            )
        )
    LEFT JOIN sugarcrm.accounts ON accounts.id = accounts_contacts.account_id
ORDER BY
    `LASTNAME`,
    `FIRSTNAME`;
4

3 回答 3

2

如果您使用 SugarCRM 框架并且您知道要查找的联系人的 ID。你可以避免所有的SQL。

$contact = BeanFactory::getBean('Contacts', $id);
$contact->account_name;

想要所有联系人?

$contact = BeanFactory::getBean('Contacts');
$all = $contact->get_full_list();
foreach ($all as $contact) {
  echo "{$contact->name} {$contact->account_name} \n <br>";
}
于 2013-06-20T21:10:05.650 回答
2
SELECT
       contacts.id AS CONTACTID,
       accounts.id AS ACCOUNTID,
       contacts.first_name,
       contacts.last_name,
       contacts.phone_work,
       contacts.phone_fax,
       contacts.department,
       contacts.title,
       contacts.description,
       contacts.salutation,
       email_addresses.email_address,
       contacts.deleted
   FROM
      contacts
         INNER JOIN accounts_contacts 
            ON contacts.id = accounts_contacts.contact_id
            JOIN accounts
               ON accounts.id = accounts_contacts.account_id
         INNER JOIN email_addr_bean_rel EABR
            ON contacts.id = EABR.bean_id
            INNER JOIN email_addresses EM
               ON EABR.email_address_id = EM.id
   WHERE 
      contacts.first_name = 'shamraiz' 

就像我帮助你解决的其他问题一样...

一次列出一个表,INNER JOIN(或 LEFT JOIN)到下一个表“ON”这两个表相关的任何标准......然后,INNER JOIN(或 LEFT JOIN)到关系层次结构中的下一个表。

如果您有同一个人的多条联系记录,例如不同的帐户和/或电子邮件,您将获得多条记录。

于 2013-06-14T15:11:23.243 回答
0

我认为您可以使用INNER JOIN查询并为表使用别名

SELECT a.`name`, c.first_name
FROM contacts c
INNER JOIN accounts_contacts ac
ON c.id = ac.contact_id
INNER JOIN accounts a
ON ac.contact_id = a.id

将给定别名添加到所有列将使数据库在执行查询时了解它们来自哪个表。

于 2013-06-14T15:14:07.597 回答