我有2个表,订阅者和发送日志。我需要选择所有订阅者,但在Sendlog表中排除符合我条件的记录。
标准是他们是“PowerUser”,SubscribePreference = 1并且他们从未收到过“GoPro”电子邮件
我有一个SQL小提琴来代表它,但无法弄清楚查询
http://sqlfiddle.com/#!3/3e1a7/6
在这个样本中我想回复Dave,Ed&弗兰克,因为从未收到过“GoPro”电子邮件
有人可以帮助我吗?
答案 0 :(得分:1)
这应该有效:
SELECT DISTINCT s.EmailAddress
, 'GoPro' AS CampaignName
, sl.SendDate
FROM Subscribers s
LEFT JOIN SendLog sl
ON s.EmailAddress = sl.EmailAddress
AND CampaignName = 'GoPro'
WHERE s.Entitlement = 'PowerUser'
AND s.SubscribePreference = 1
AND sl.EmailAddress IS NULL
答案 1 :(得分:0)
我相信这会奏效:
SELECT DISTINCT Subscribers.EmailAddress
FROM Subscribers
LEFT OUTER JOIN SendLog
ON Subscribers.EmailAddress = SendLog.EmailAddress
AND SendLog.CampaignName = 'GoPro'
WHERE SendLog.EmailAddress IS NULL
AND Subscribers.SubscribePreference = 1
And Subscribers.Entitlement = 'PowerUser'
出于性能原因,我建议将一个ID列作为主键添加到Subscribers表,然后在SendLog表中创建一个名为SubscriberID的外键,并在其上加入它们。上面的查询将起作用,但使用整数和适当的键会更快。