MySQL Left Join 产生空行

MySQL Left Join producing null rows

我正在尝试使用下面的查询来过滤相当大的 table 并收集有关已更新数据的用户的数据(带有用于更新的用户 ID 的 table 是内置的触发器并每天清理)。因此,目标是以下列格式返回数据:

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

通过这种方式,我们可以知道哪些用户已更新,但尚未进行交易。

但是,当左侧查询的另一侧没有匹配项(即未找到最大值 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    |
|       ...           ...               ...            ...               ...     |
+--------------------------------------------------------------------------------+

我不确定为什么左连接将无法连接的行的所有值都设置为 null,特别是因为 LEFT table 确实有一个 web_contacts_id这些用户。这是我目前正在使用的查询。

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

您 select 中的所有列都来自 wclwc 表,因此当 bld 中有一行没有匹配 wcl 行,你会得到一行 nulls.

您可以 select bld.id 而不是 wcl.web_contacts_id 来至少获取以下行中的一些内容:

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