简体   繁体   中英

MySQL Left Join producing null rows

I am trying to use the query below to filter a rather large table and gather up data on users who have updated data (the table with user ids for the updates is built ala triggers and cleared out daily). As such, the goal is to have data returned in the following format:

+--------------------------------------------------------------------------------+
| web_contacts_id   contact_id    franchise_id    last_transaction_date    email |
+--------------------------------------------------------------------------------+
|        1             23                1            1/1/15          bob@bob.bob|
|        2            null              null           null              null    |
|       ...           ...               ...            ...               ...     |
+--------------------------------------------------------------------------------+

This way, we can tell which users have been updated, but have not had a transaction yet.

However, the query is producing the following results when the other side of the left query has nothing to match on (ie, did not find the max last_transaction_date)

+--------------------------------------------------------------------------------+
| web_contacts_id   contact_id    franchise_id    last_transaction_date    email |
+--------------------------------------------------------------------------------+
|        1             23                1            1/1/15          bob@bob.bob|
|       null          null              null           null              null    |
|       ...           ...               ...            ...               ...     |
+--------------------------------------------------------------------------------+

I'm not sure why the left join is setting all values to null for a row that it is not able to join on, especially since the LEFT table does have a web_contacts_id for these users. Here is the query I am currently using.

SELECT wcl.web_contacts_id, wcl.contact_id, wcl.franchise_id, wcl.last_transaction_date, wc.email
FROM bl_updates bld
LEFT JOIN (
    SELECT wcl.contact_id, wcl.franchise_id, wcl.web_contacts_id, wcl2.last_transaction_date
    FROM web_contacts_location wcl
    INNER JOIN (
        SELECT wcl_inner.web_contacts_id, MAX(wcl_inner.last_transaction_date) as last_transaction_date
        FROM web_contacts_location as wcl_inner
        WHERE web_contacts_id IN (
            SELECT id FROM bl_updates
        )
        GROUP BY web_contacts_id
    ) wcl2 on wcl.web_contacts_id = wcl2.web_contacts_id AND wcl.last_transaction_date = wcl2.last_transaction_date
)wcl ON wcl.web_contacts_id = bld.id
LEFT JOIN web_contacts wc on wc.id = wcl.web_contacts_id

All the columns you're selecting are from the wcl and wc tables, so when there's a row in bld that doesn't have a matching wcl row, you'll get a row of null s.

You could select bld.id instead of wcl.web_contacts_id to get at least some content in these rows:

SELECT bld.id, wcl.contact_id, wcl.franchise_id, wcl.last_transaction_date, wc.email
-- Here --^
FROM bl_updates bld
LEFT JOIN (
    SELECT wcl.contact_id, wcl.franchise_id, wcl.web_contacts_id, wcl2.last_transaction_date
    FROM web_contacts_location wcl
    INNER JOIN (
        SELECT wcl_inner.web_contacts_id, MAX(wcl_inner.last_transaction_date) as last_transaction_date
        FROM web_contacts_location as wcl_inner
        WHERE web_contacts_id IN (
            SELECT id FROM bl_updates
        )
        GROUP BY web_contacts_id
    ) wcl2 on wcl.web_contacts_id = wcl2.web_contacts_id AND wcl.last_transaction_date = wcl2.last_transaction_date
)wcl ON wcl.web_contacts_id = bld.id
LEFT JOIN web_contacts wc on wc.id = wcl.web_contacts_id

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