创建一个包含多个表的视图?
Creating a view with multiple tables?
我正在尝试创建一个包含 5 个 table 的视图,但是我在尝试将最后两个 table 连接在一起时遇到问题。
这是我当前的 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
我的 table 设置如下:
帐户(id、first_name、last_name 等)
联系人(id、姓名等)
ACCOUNT_CONTACTS (id, account_id, contact_id)
EMAIL_ADDR_BEAN_REL (id, bean_id (等同于 account_id / contact_id), email_address_id)
EMAIL_ADDRESSES (email_address_id, email_address)
我遇到的问题是 EMAIL_ADDR_BEAN_REL table 有一个可以引用 account_id 或 contact_id 的 bean_id。因此,当我加入 table 时,它只有 return 帐户电子邮件地址,但我希望它同时 return 帐户电子邮件地址和联系人电子邮件地址?
我觉得我加入 table 的方式有问题,但我不确定如何前进?
谢谢!
您可以为 EMAIL_ADDR_BEAN_REL table 设置 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
编辑
由于您不必为帐户或联系人设置一行(如果我从上面的评论中理解正确 - 谢谢,@BK435),现在将其设置为 LEFT JOIN。我还添加了 bean_module
列的指定(尽管我不确定您为此使用的是什么代码)。
我正在尝试创建一个包含 5 个 table 的视图,但是我在尝试将最后两个 table 连接在一起时遇到问题。
这是我当前的 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
我的 table 设置如下:
帐户(id、first_name、last_name 等)
联系人(id、姓名等)
ACCOUNT_CONTACTS (id, account_id, contact_id)
EMAIL_ADDR_BEAN_REL (id, bean_id (等同于 account_id / contact_id), email_address_id)
EMAIL_ADDRESSES (email_address_id, email_address)
我遇到的问题是 EMAIL_ADDR_BEAN_REL table 有一个可以引用 account_id 或 contact_id 的 bean_id。因此,当我加入 table 时,它只有 return 帐户电子邮件地址,但我希望它同时 return 帐户电子邮件地址和联系人电子邮件地址?
我觉得我加入 table 的方式有问题,但我不确定如何前进?
谢谢!
您可以为 EMAIL_ADDR_BEAN_REL table 设置 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
编辑
由于您不必为帐户或联系人设置一行(如果我从上面的评论中理解正确 - 谢谢,@BK435),现在将其设置为 LEFT JOIN。我还添加了 bean_module
列的指定(尽管我不确定您为此使用的是什么代码)。