MySQL Left Join生成空行

时间:2015-06-18 16:18:17

标签: mysql sql select left-join

我正在尝试使用下面的查询来过滤相当大的表并收集有更新数据的用户的数据(具有用户ID的表用于更新是构建ala触发器并且每天清除)。因此,目标是以下列格式返回数据:

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

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

但是,如果左侧查询的另一侧没有任何内容可匹配(即未找到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    |
|       ...           ...               ...            ...               ...     |
+--------------------------------------------------------------------------------+

我不确定为什么左连接为无法连接的行将所有值设置为null,尤其是因为LEFT表确实为这些用户提供了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

1 个答案:

答案 0 :(得分:2)

您选择的所有列都来自wclwc表格,因此当bld中有一行没有列时匹配wcl行,您将获得一行null

您可以选择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