一尘不染

选择未发送时事通讯的用户

mysql

尝试创建查询以选择批次超时时未通过电子邮件发送新闻通讯的用户。mail_log表从3个不同的邮件列表中捕获条目,并保持过去4-5周的日志-
换句话说,每个订户应有多个日志条目。

我要选择在发送时批处理超时时未通过电子邮件发送的所有订阅者。

mail_log
+--------+------------+-------------+------------+---------+
| log_id | send_date  | location_id | mailing_id | list_id |
+--------+------------+-------------+------------+---------+
location_id is which mailing list
mailing_id is the specific newsletter
list_id is the subscriber's id in the mailing list

mail_list 
+---------+-------+-------+-------+
| list_id | fname | lname | email |
+---------+-------+-------+-------+

我已经尝试过以下查询:

SELECT mail_list.*
FROM mail_list
LEFT JOIN mail_log ON mail_log.list_id = mail_list.list_id
WHERE mail_log.send_date = '2016-07-12'
AND mail_log.location_id = '2'
AND mail_log.list_id IS NULL`

该查询返回0个结果,但成功的查询应返回大约700个结果。


阅读 211

收藏
2020-05-17

共1个答案

一尘不染

使用时LEFT JOIN,必须将对子表的限制放入ON子句中。否则,当您测试这些字段时,您将仅匹配非NULL行,这与AND mail_log.list_id IS NULL测试矛盾。

SELECT mail_list.*
FROM mail_list
LEFT JOIN mail_log ON mail_log.list_id = mail_list.list_id
    AND mail_log.send_date = '2016-07-12'
    AND mail_log.location_id = '2'
WHERE mail_log.list_id IS NULL
2020-05-17