问题
I am trying to retrieve the name of the compnay a contact belongs to. the relation ships exists in the table account_contacts
, how ever when I try to tun the query it barks
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
error i get is
[Err] 1054 - Unknown column 'contacts.id' in 'on clause'
AFTER changes:
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'
your query returns 2 rows with the results i expect. the account id are different. However the query I have redone again implementing it your way does not work. the accountid is the same, but returns 2 rows.
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'
the next query returns 3 rows, but the top 2 are dupplicated
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'
from contacts
SELECT * FROM
sugarcrm.
contactswhere first_name = 'shamraiz'
returns 2 rows
from account_contact relation
SELECT * FROM
sugarcrm.
accounts_contactswhere contact_id = '17619b5e-db07-fa3b-6748-51a73ef38c5e'
returns 1 row
SELECT * FROM
sugarcrm.
accounts_contactswhere contact_id = '003b0000006ZMDXAA4'
returns 1 row.
So the final query should return 2 different rows as they are two contacts with similar names joined to 2 different companies.
A contact can belong to 1 company.
more adjustments:
I have made some amendments but it is returning 1 record. should return 2. I need it to pull out the record whether or not a relationship exists for email address.
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'
resolved answer:
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`;
回答1:
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'
Just like the other questions I've helped you on...
List one table at a time, INNER JOIN (or LEFT JOIN) to the next table "ON" whatever criteria that those two tables relate... Then, INNER JOIN (or LEFT JOIN) to the next table in the relationship hierarchy.
If you have multiple contact records for the same person, such as different accounts and/or emails, you WILL get multiple records.
回答2:
If you use the SugarCRM framework and you know the id of the contact you are looking for. You can avoid all of the SQL.
$contact = BeanFactory::getBean('Contacts', $id);
$contact->account_name;
Want all contacts?
$contact = BeanFactory::getBean('Contacts');
$all = $contact->get_full_list();
foreach ($all as $contact) {
echo "{$contact->name} {$contact->account_name} \n <br>";
}
回答3:
I think you can use an INNER JOIN
query and use alias for tables
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
Prepending the given alias to all the column will make database understand what table are them from while executing the query.
来源:https://stackoverflow.com/questions/17111649/sugarcrm-simple-query-not-working