创建视图?
Creating a view?
目前我有一个将多个表连接在一起的视图,这些表按以下方式设置:
ACCOUNTS (id, first_name, last_name, etc.)
CONTACTS (id, name, etc.)
ACCOUNT_CONTACTS (id, account_id, contact_id)
EMAIL_ADDR_BEAN_REL (id, bean_id (same as account_id / contact_id), email_address_id)
EMAIL_ADDRESSES (email_address_id, email_address)
但是我有两个连接在一起的表,其中可以有多个电子邮件与一个 ID 相关联,这里是结构的示例:
Table: EMAIL_ADDR_BEAN_REL
id email_address_id bean_id
1 2d5b8 61720
2 14355b 61720
Table: EMAIL_ADDRESSES
id email_address
2db58 WalterWhite@gmail.com
14355b WW@gmail.com
在创建我的视图时,我希望能够检索与一个用户关联的这些多封电子邮件。目前这是我的视图代码,它只检索一封电子邮件:
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, C.last_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, EA.email_address, EA_C.email_address as 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"
LEFT JOIN EMAIL_ADDR_BEAN_REL AS ER_C ON ER_C.bean_id = C.id
AND ER_C.bean_module = "Contacts"
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
此查询将 return 与同一用户关联的两行,除了电子邮件地址外具有完全相同的信息,我想将第二个电子邮件地址(或其他电子邮件)添加到同一行并且没有创建其他行。
不胜感激!
SELECT ... GROUP_CONCAT(email_address) ...
FROM ...
GROUP BY A.id;
目前我有一个将多个表连接在一起的视图,这些表按以下方式设置:
ACCOUNTS (id, first_name, last_name, etc.)
CONTACTS (id, name, etc.)
ACCOUNT_CONTACTS (id, account_id, contact_id)
EMAIL_ADDR_BEAN_REL (id, bean_id (same as account_id / contact_id), email_address_id)
EMAIL_ADDRESSES (email_address_id, email_address)
但是我有两个连接在一起的表,其中可以有多个电子邮件与一个 ID 相关联,这里是结构的示例:
Table: EMAIL_ADDR_BEAN_REL
id email_address_id bean_id
1 2d5b8 61720
2 14355b 61720
Table: EMAIL_ADDRESSES
id email_address
2db58 WalterWhite@gmail.com
14355b WW@gmail.com
在创建我的视图时,我希望能够检索与一个用户关联的这些多封电子邮件。目前这是我的视图代码,它只检索一封电子邮件:
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, C.last_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, EA.email_address, EA_C.email_address as 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"
LEFT JOIN EMAIL_ADDR_BEAN_REL AS ER_C ON ER_C.bean_id = C.id
AND ER_C.bean_module = "Contacts"
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
此查询将 return 与同一用户关联的两行,除了电子邮件地址外具有完全相同的信息,我想将第二个电子邮件地址(或其他电子邮件)添加到同一行并且没有创建其他行。
不胜感激!
SELECT ... GROUP_CONCAT(email_address) ...
FROM ...
GROUP BY A.id;