2015-05-15 96 views
0

我有一個MySQL查詢(我使用phpmyadmin),我知道一個表不應該像這樣創建,但我必須只有一個表我爲之工作的項目。這是我的sql查詢。MySql表如何獲取所有聯繫人?

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

那裏有不止一張桌子。 – Uueerdo

+0

是的,有多個,但我主要關心的是帳戶,聯繫人和account_contacts。一會兒我會粘貼一張表格的圖片。 – ChewyMasta

+0

賬戶表:http://i.stack.imgur.com/rK2lN.png – ChewyMasta

回答

0

您正在使用GROUP BY A.ID.它只會顯示與該ID相對應的一條記錄。所以只顯示一個聯繫人。 所以,你可以使用GROUP BY C.ID,A.ID

+0

好的,但我想顯示所有聯繫人及其關聯公司。在這種情況下,A.id是公司。那麼我怎樣才能讓它顯示所有聯繫人和公司。即使有公司信息的重複? – ChewyMasta

+0

更改組中的列順序。 GROUP BY C.EMAIL_ID,A.ID –

+0

好吧我現在正在使用AC.id進行分組,但它給了我一個額外的聯繫人(最後一個輸入)的副本? – ChewyMasta

0

我還不能肯定,但我認爲你也許可以簡化您的查詢相當多的(消除EMAIL_ADDR_BEAN_RELEMAIL_ADDRESSES多種用途)用更加複雜的連接條件加入到EMAIL_ADDR_BEAN_REL

東西類似於下面的片段。我稍微改變了邏輯,猜測一些可能的錯誤或不必要的條件; GUID使用,你可能並不需要知道「豆模塊」的提示:

... 
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條款中這樣做,以及東西。

相關問題