我正在尝试检索联系人所属的compnay的名称。表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行
SELECT * FROM
sugarcrm .
accounts_contacts where contact_id = '17619b5e-db07-fa3b-6748-51a73ef38c5e'
返回1行
SELECT * FROM
sugarcrm .
accounts_contacts where 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`;
答案 0 :(得分: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)到关系中的下一个表层次结构。
如果您有同一个人的多个联系人记录,例如不同的帐户和/或电子邮件,您将获得多条记录。
答案 1 :(得分: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>";
}
答案 2 :(得分: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
将给定别名预先添加到所有列中将使数据库在执行查询时了解它们是什么表。