我有一个MySQL查询(我使用phpmyadmin),我知道不应该像这样创建一个表,但我必须只有一个表用于我工作的项目。这是我的SQL查询。
CREATE VIEW v_sugarcrm AS
SELECT AC.id, A.id AS account_id, A.name, A.description, A.industry, A.phone_fax, A.phone_office, A.shipping_address_street, A.shipping_address_city, A.shipping_address_state, A.shipping_address_postalcode, A.shipping_address_country, C.id AS contact_id, C.first_name, SUBSTRING_INDEX(C.last_name, ' ', -1) as last_name,concat(c.first_name, ' ', c.last_name) as full_name, SUBSTRING_INDEX(SUBSTRING_INDEX(concat(c.first_name, ' ', c.last_name), ' ', 2), ' ', -1) as middle_name, C.title, C.department, C.phone_home, C.phone_mobile, C.phone_work, C.primary_address_street, C.primary_address_city, C.primary_address_state, C.primary_address_postalcode, C.primary_address_country, C.deleted, EA.email_address as email_account,
SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(EA_C1.email_address), ',', 1), ',', -1) as email_2,
If(length(GROUP_CONCAT(EA_C1.email_address)) - length(replace(GROUP_CONCAT(EA_C1.email_address), ',', ''))>=1,
SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(EA_C1.email_address), ',', 2), ',', -1), NULL) as email_3,
If(length(GROUP_CONCAT(EA_C1.email_address)) - length(replace(GROUP_CONCAT(EA_C1.email_address), ',', ''))>=2,
SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(EA_C1.email_address), ',', 3), ',', -1), NULL) as email_4,
If(length(GROUP_CONCAT(EA_C1.email_address)) - length(replace(GROUP_CONCAT(EA_C1.email_address), ',', ''))>=3,
SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(EA_C1.email_address), ',', 4), ',', -1), NULL) as email_5,
EA_C.email_address as email_contact,
GROUP_CONCAT(EA_C1.email_address) as alt_email_contact
FROM ACCOUNTS A
INNER JOIN ACCOUNTS_CONTACTS AS AC ON A.id = AC.account_id
INNER JOIN CONTACTS AS C ON C.id = AC.contact_id
LEFT JOIN EMAIL_ADDR_BEAN_REL AS ER ON ER.bean_id = A.id
AND ER.bean_module = "Accounts" AND ER.primary_address =1
LEFT JOIN EMAIL_ADDR_BEAN_REL AS ER_C ON ER_C.bean_id = C.id
AND ER_C.bean_module = "Contacts" AND ER_C.primary_address = 1
LEFT JOIN EMAIL_ADDR_BEAN_REL AS ER_C1 ON ER_C1.bean_id = C.id and ER_C1.deleted !=1
AND ER_C1.primary_address !=1
LEFT JOIN EMAIL_ADDR_BEAN_REL AS ER_A1 ON ER_A1.bean_id = A.id AND ER_A1.deleted !=1
and ER_A1.primary_address !=1
LEFT JOIN EMAIL_ADDRESSES AS EA ON ER.email_address_id = EA.id
LEFT JOIN EMAIL_ADDRESSES AS EA_C ON ER_C.email_address_id = EA_C.id
LEFT JOIN EMAIL_ADDRESSES AS EA_C1 ON ER_C1.email_address_id = EA_C1.id
AND EA_C1.invalid_email !=1 AND EA_C1.opt_out !=1
LEFT JOIN EMAIL_ADDRESSES AS EA_A1 ON ER_A1.email_address_id = EA_A1.id
AND EA_A1.invalid_email !=1 AND EA_A1.opt_out !=1
GROUP BY A.ID
Account_Contacts表 http://i.stack.imgur.com/KUECA.png
我的主要问题围绕着我在查询顶部的两个内部联接,截至目前,此查询为我提供了联系人和公司所需的所有信息,但是当有多个联系人与一家公司只显示一个联系人,而不是两个联系人。我想知道如何更改我的查询,以便显示所有联系人,即使公司的信息相同,联系人的信息也应该不同,他们也应该有不同的ID。
我真的需要帮助!
答案 0 :(得分:0)
您正在使用GROUP BY A.ID.它只显示与该ID对应的一条记录。因此只显示一个联系人。 所以你可以使用GROUP BY C.ID,A.ID
答案 1 :(得分:0)
我不是肯定的,但我认为您可以简单地简化您的查询(消除EMAIL_ADDR_BEAN_REL
AND EMAIL_ADDRESSES
)的多种用途,并使用更复杂的连接条件加入EMAIL_ADDR_BEAN_REL
。
类似下面的片段。我稍微改变了逻辑,猜测了一些可能的错误或不必要的条件; GUID使用提示,您可能不需要知道" bean模块":
...
FROM ACCOUNTS A
INNER JOIN ACCOUNTS_CONTACTS AS AC ON A.id = AC.account_id
INNER JOIN CONTACTS AS C ON C.id = AC.contact_id
LEFT JOIN EMAIL_ADDR_BEAN_REL AS ER
ON ER.bean_id IN (A.id, C.id)
AND (ER.primary_address = 1 OR ER.deleted != 1)
LEFT JOIN EMAIL_ADDRESSES AS EA ON ER.email_address_id = EA.id
...
我怀疑它可能会简化你在SELECT
子句中所做的所有事情。