当前位置: 代码迷 >> Sql Server >> not in 连用null 注意啥?解决办法
  详细解决方案

not in 连用null 注意啥?解决办法

热度:35   发布时间:2016-04-24 10:43:38.0
not in 连用null 注意啥??
sql server 使用 not in 时 要注意null值问题 ?
求大神讲讲  两者的关系联系??
------解决方案--------------------
select 1   where 2 not in (3,null)  --没有输出结果集
select 1   where 2   in (2,null)    --输出1

------解决方案--------------------
本帖最后由 DBA_Huangzj 于 2014-04-10 15:46:40 编辑
T-SQL是三值逻辑,true、false和unknown,not true=false,not false=true,not unknown=unknown.
另外,in的本质就是or,or是短路逻辑,只要其中一个为true,整个谓词就为true,
所以:select 1   where 2   in (2,null)  为true,返回1
而:select 1   where 2 not in (3,null) 
2不等于3,同时2和null也不能匹配,所以为false
  相关解决方案