sql – 对多个表使用NOT IN

如何简化多个“不在”查询?使用多个子查询是否有效:不在(…)和不在(..)和不在(…) 我正在使用计数(抱歉忘记了) Select count (VisitorID) from Company where VisitorID not in (select VisitorID from UserLog where ActionID = 2 ) and VisitorID 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是唯一正确的选项

作者: dawei

【声明】:西安站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部