此查询的工作原理与我预期的一样,但我试图避免使用“not in”:
select * from subscribers ls join emailopens eo
on ls.subscriberid=eo.subscriberid
where ls.listid=1381
and opentime > 1458864000
and emailaddress not IN (select emailaddress from subscribers where listid=1384)
我试过这个:
select * from subscribers ls join emailopens eo
on ls.subscriberid=eo.subscriberid left join subscribers ls2
on ls.emailaddress=ls2.emailaddress
where ls.listid=1381
and opentime > 1458864000
and ls2.listid=1384
问题是它只返回listid=1381
和listid=1384
中具有相同电子邮件的行。我想,因为我使用左连接它应该返回所有行where listid=1381
,然后在1384
中同一封电子邮件的行上将有数据,但如果1381
中有电子邮件1}}而非1384
,会显示null
列中的ls2.*
。
答案 0 :(得分:1)
将ls2.listid
放入WHERE
有效地将LEFT JOIN
变为INNER JOIN
。尝试在listid
中保留ON
约束,然后,为了产生NOT IN
的效果,通过询问ls2.emailaddress IS NULL
:<的记录,使其成为反连接/ p>
select * from subscribers ls join emailopens eo
on ls.subscriberid=eo.subscriberid left join subscribers ls2
on ls.emailaddress=ls2.emailaddress and ls2.listid = 1384
where ls.listid=1381
and opentime > 1458864000
and ls2.emailaddress is null