https://stackoverflow.com/questions/129077/not-in-clause-and-null-values

This issue came up when I got different records counts for what I thought were identical queries one using a not in where constraint and the other a left join. The table in the not in constraint had one null value (bad data) which caused that query to return a count of 0 records. I sort of understand why but I could use some help fully grasping the concept.

To state it simply, why does query A return a result but B doesn't?

A: select 'true' where 3 in (1, 2, 3, null)
B: select 'true' where 3 not in (1, 2, null)

This was on SQL Server 2005. I also found that calling set ansi_nulls off causes B to return a result.

答案:

Query A is the same as:

select 'true' where 3 = 1 or 3 = 2 or 3 = 3 or 3 = null

Since 3 = 3 is true, you get a result.

Query B is the same as:

select 'true' where 3 <> 1 and 3 <> 2 and 3 <> null

When ansi_nulls is on, 3 <> null is UNKNOWN, so the predicate evaluates to UNKNOWN, and you don't get any rows.

When ansi_nulls is off, 3 <> null is true, so the predicate evaluates to true, and you get a row.

NOT IN clause and NULL values的更多相关文章

  1. MySQL :: MySQL 8.0 Reference Manual :: B.6.4.3 Problems with NULL Values https://dev.mysql.com/doc/refman/8.0/en/problems-with-null.html

    MySQL :: MySQL 8.0 Reference Manual :: B.6.4.3 Problems with NULL Values https://dev.mysql.com/doc/r ...

  2. Retain NULL values vs Keep NULLs in SSIS Dataflows - Which To Use? (转载)

    There is some confusion as to what the various NULL settings all do in SSIS. In fact in one team whe ...

  3. You can add an index on a column that can have NULL values if you are using the MyISAM, InnoDB, or MEMORY storage engine.

    w https://dev.mysql.com/doc/refman/5.7/en/create-index.html MySQL :: MySQL 5.7 Reference Manual :: B ...

  4. SQL NULL Values

    NULL代表缺失的.未知的数据.表的列值默认是NULL.如果某个表的某个列不是NOT NULL的,那么当我们插入新纪录.更新已存在的记录时,可以不用为此列赋值,这意味着那个列保存为NULL值. NUL ...

  5. Elasticsearch的null values

    很多时候,我们需要面临null值的烦扰,查询es时传入null值是要查询出null的数据还是不查这个field呢,稍有不慎就会引发新的bug,这的确是个问题! null_value 意味着无法索引或搜 ...

  6. Have You Ever Wondered About the Difference Between NOT NULL and DEFAULT?

    https://blog.jooq.org/2014/11/11/have-you-ever-wondered-about-the-difference-between-not-null-and-de ...

  7. mysql 变量is null 和 not exists区别

    问题: 使用游标遍历时,发现使用 select var into tmp where var=? 然后判断if tmp is null时,不能走完所有的遍历.经debug发现, 当var为空时,则跳出 ...

  8. MySQL Case--Strict mode与NOT NULL

    事故回溯 某业务流程操作为: 1.循环扫描某张待处理请求表,查看是否有请求等待处理. 2.找到待处理请求后,申请相关资源进行处理,并将处理结果插入到处理结果表中. 3.将该请求从待处理请求表中移除. ...

  9. TSQL 聚合函数忽略NULL值

    max,min,sum,avg聚合函数会忽略null值,但不代表聚合函数不返回null值,如果表为空表,或聚合列都是null,则返回null.count 聚合函数忽略null值,如果聚合列都是null ...

随机推荐

  1. POJ2560 Freckles

    Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description In an epis ...

  2. 【AIM Tech Round 5 (Div. 1 + Div. 2) 】

    A:https://www.cnblogs.com/myx12345/p/9844152.html B:https://www.cnblogs.com/myx12345/p/9844205.html ...

  3. Oracle 实现 一个关键字 匹配多个 字段

    有这么一个需求,满足只有一个输入框的条件下,支持不同数据列的搜索结果. 说白了,就是这个 输入框  既可以用来 搜索姓名,也可以搜索 年龄,地址等. 分析: 一般情况下,我们的一个输入框对应 数据库 ...

  4. T2602 最短路径问题 codevs

    http://codevs.cn/problem/2602/ 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold   题目描述 Description 平面上有n个点(n& ...

  5. intellij idea springmvc web工程之helloworld

    1.新建java工程 2.设置项目 2.添加jar包 3.配置web.xml <?xml version="1.0" encoding="UTF-8"?& ...

  6. HBase总结

    1.HBase是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”.就像Bigtable利用了Google ...

  7. 临远的spring security教程

    为啥选择Spring Security 欢迎阅读咱们写的Spring Security教程,咱们既不想写一个简单的入门教程,也不想翻译已有的国外教程.咱们这个教程就是建立在咱们自己做的OA的基础上,一 ...

  8. .net core mvc启动顺序以及主要部件1

    原文:.net core mvc启动顺序以及主要部件1 首先我是新人一个写这些东西也是为了增加记忆,有不对的地方请多多指教. 说回正题,打开Program.cs文件,看到在有个CrateWebHost ...

  9. 第三讲_图像特征与描述Image Feature Descriptor

    第三讲_图像特征与描述Image Feature Descriptor 概要 特征提取方法 直方图 对图片数据/特征分布的一种统计:对不同量进行直方图统计:可以表示灰度,颜色,梯度,边缘,形状,纹理, ...

  10. how to avoid over-fitting?(机器学习中防止过拟合的方法,重要)

    methods to avoid overfitting: Cross-Validation : Cross Validation in its simplest form is a one roun ...