我有一個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。
我真的需要幫助!
那裏有不止一張桌子。 – Uueerdo
是的,有多個,但我主要關心的是帳戶,聯繫人和account_contacts。一會兒我會粘貼一張表格的圖片。 – ChewyMasta
賬戶表:http://i.stack.imgur.com/rK2lN.png – ChewyMasta