简体   繁体   中英

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.

Here is my current MySQL query:

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.)

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)

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. 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):

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

EDIT

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. I've also added the specifying of the bean_module column (although I'm not sure what code you are using for that).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM