使用 JOIN 而不是 NOT IN 消除记录
Eliminate records using a JOIN instead of NOT IN
此查询按我预期的方式工作,但我试图避免使用 "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
问题是它只有 returns 在 listid=1381
和 listid=1384
中具有相同电子邮件的行。我想因为我使用的是左连接,所以它应该 return 所有行 where listid=1381
,然后在相同电子邮件位于 1384
的行上会有数据,但是如果有1381
而非 1384
中的电子邮件将显示 ls2.*
列中的 null
。
将 ls2.listid
放入 WHERE
有效地将 LEFT JOIN
变成 INNER JOIN
。尝试在 ON
中保留 listid
约束,然后,为了产生 NOT IN
的效果,通过询问 ls2.emailaddress IS NULL
中的记录使其成为反连接:
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
此查询按我预期的方式工作,但我试图避免使用 "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
问题是它只有 returns 在 listid=1381
和 listid=1384
中具有相同电子邮件的行。我想因为我使用的是左连接,所以它应该 return 所有行 where listid=1381
,然后在相同电子邮件位于 1384
的行上会有数据,但是如果有1381
而非 1384
中的电子邮件将显示 ls2.*
列中的 null
。
将 ls2.listid
放入 WHERE
有效地将 LEFT JOIN
变成 INNER JOIN
。尝试在 ON
中保留 listid
约束,然后,为了产生 NOT IN
的效果,通过询问 ls2.emailaddress IS NULL
中的记录使其成为反连接:
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