[英]Creating a view with multiple tables?
I am trying to create a view with 5 tables however I am having issues when trying to join the last two tables together. 我正在尝试创建具有5个表的视图,但是在尝试将最后两个表连接在一起时遇到了问题。
Here is my current MySQL query: 这是我当前的MySQL查询:
CREATE VIEW s_view 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, 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
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
INNER JOIN EMAIL_ADDR_BEAN_REL AS ER ON ER.bean_id = A.id
INNER JOIN EMAIL_ADDRESSES AS EA ON ER.email_address_id = EA.id
And my tables are set up in the following way: 我的表是通过以下方式设置的:
ACCOUNTS (id, first_name, last_name, etc.) ACCOUNTS(ID,名字,姓氏等)
CONTACTS (id, name, etc.) 联系人(ID,姓名等)
ACCOUNT_CONTACTS (id, account_id, contact_id) ACCOUNT_CONTACTS(id,account_id,contact_id)
EMAIL_ADDR_BEAN_REL (id, bean_id (same as account_id / contact_id), email_address_id) EMAIL_ADDR_BEAN_REL(ID,Bean_ID(与account_id / contact_id相同),email_address_id)
EMAIL_ADDRESSES (email_address_id, email_address) EMAIL_ADDRESSES(电子邮件地址ID,电子邮件地址)
The issue I am having is that the EMAIL_ADDR_BEAN_REL table has a bean_id that can reference either an account_id or a contact_id. 我遇到的问题是EMAIL_ADDR_BEAN_REL表具有可以引用account_id或contact_id的bean_id。 Therefore when I join the tables it only returns the accounts email address however i want it to return both the accounts email address and the contacts email address? 因此,当我加入表格时,它仅返回帐户电子邮件地址,但是我希望它同时返回帐户电子邮件地址和联系人电子邮件地址吗?
I have a feeling it is an issue with the way i am joining the tables but i am unsure as how to move forward? 我感觉这是我加入餐桌的方式的一个问题,但是我不确定该如何前进?
Thanks! 谢谢!
You can just set up 2 separate aliases for the EMAIL_ADDR_BEAN_REL table and reference it appropriately (because of your structure you have to add another aliased version of email_address as well): 您可以为EMAIL_ADDR_BEAN_REL表设置2个单独的别名并适当地引用它(由于您的结构,您还必须添加另一个别名版本的email_address):
CREATE VIEW s_view 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, 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_address_c
FROM ACCOUNTS A
INNER JOIN ACCOUNTS_CONTACTS AS AC ON A.id = AC.contact_id
INNER JOIN CONTACTS AS C ON C.id = AC.account_id
LEFT JOIN EMAIL_ADDR_BEAN_REL AS ER ON ER.bean_id = AC.id
AND ER.bean_module = 'accounts'
LEFT JOIN EMAIL_ADDR_BEAN_REL AS ER_C ON ER.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
Since you do not necessarily have a row for accounts or for contacts (if I understand correctly from the comments above - thanks, @BK435) it is now set up as a LEFT JOIN. 由于您不一定要为客户或联系人排一行(如果我从上面的注释中正确理解-谢谢,@ BK435),现在将其设置为“左联接”。 I've also added the specifying of the bean_module
column (although I'm not sure what code you are using for that). 我还添加了bean_module
列的指定(尽管我不确定您使用的是什么代码)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.