sql – 对多个表使用NOT IN

如何简化多个“不在”查询?使用多个子查询是否有效:不在(…)和不在(..)和不在(…)

我正在使用计数(抱歉忘记了)

Select count (VisitorID)

 from Company

 where VisitorID not in (select VisitorID from UserLog where ActionID = 2 )

 and VisitorID not in (select VisitorID from Supplies where productID = 4)

解决方法

Select count (VisitorID)

from Company C
where
NOT EXISTS (select * from UserLog U where ActionID = 2 AND C.VisitorID  = U.VisitorID)
AND
NOT EXISTS (select * from Supplies S where productID = 4 AND S.VisitorID  = U.VisitorID)

为什么不存在?

> NOT IN:UserLog或Supplies中的任何NULL VisitorID值表示不匹配
>(LEFT JOIN):如果每个VisitorID有多个UserLog或Supplies,则为多个输出行.需要disTINCT改变计划

通常,NOT EXISTS是唯一正确的选项

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...