我正在尝试检索联系人所属公司的名称。关系存在于表中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 * FROM
Sugarcrm.
联系人where first_name = 'shamraiz'
返回 2 行
来自 account_contact 关系
SELECT * FROM
sugarcrm .
accounts_contactswhere contact_id = '17619b5e-db07-fa3b-6748-51a73ef38c5e'
返回 1 行
SELECT * FROM
Sugarcrm .
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`;