`
dingjun1
  • 浏览: 207932 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

not in null 与null运算

阅读更多
Not in \ in中包含有null值的列,不会排除null(因为 null != null)
Oracle not in (如果子查询返回的值或是集合有null值的话,将查询不到记录

解决办法: 子查询中限制不能游null返回值 is not null

用 not exists 代替 not in


True and null 返回 null
true or null 返回true ,
false and null返回null
false or null 返回null

null要用is进行比较如果用其它的比较运算符都返回null,
如:(1<>null返回null 1=null返回null)

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.


分享到:
评论

相关推荐

    Mysql实现null值排在最前/最后的方法示例

    IS NOT NULL: 当列的值不为 NULL, 运算符返回 true。 &lt;=&gt;: 比较操作符(不同于=运算符),当比较的的两个值为 NULL 时返回 true。 关于 NULL 的条件比较运算是比较特殊的。你不能使用 = NULL 或 != NULL 在列中...

    MySQL NULL 值处理

    IS NOT NULL: 当列的值不为 NULL, 运算符返回 true。 : 比较操作符(不同于 = 运算符),当比较的的两个值相等或者都为 NULL 时返回 true。 关于 NULL 的条件比较运算是比较特殊的。你不能使用 = NULL 或 != ...

    MySQL NULL 值处理实例详解

    IS NOT NULL: 当列的值不为NULL, 运算符返回true。 &lt;=&gt;: 比较操作符(不同于=运算符),当比较的的两个值为NULL时返回true。 关于 NULL 的条件比较运算是比较特殊的。你不能使用 = NULL 或 != NULL 在列中查找 ...

    mysql中is null语句的用法分享

    对null的特殊处理即是在前面的章节中,为了决定哪个动物不再是活着的,使用death is not null而不使用death != null的原因。 在group by中,两个null值视为相同。 执行order by时,如果运行 order by … asc,则null...

    mind1949#advanced-sql-tutorial#1.7.4_用差集实现关系除法运算1

    1.7.4 用差集实现关系除法运算准备数据:skill varchar(100) not nullemp varchar(100) not null,skill

    详解JavaScript逻辑Not运算符

    在JavaScript 中,逻辑NOT运算符与C和Java中的逻辑 NOT 运算符相同,都由感叹号(!... 如果运算数是 null,返回 true 如果运算数是 NaN,返回 true 如果运算数是 undefined,发生错误 测试脚本如下: [removed]

    SQL 基础--SELECT 查询

    --与NULL运算,结果为NULL SQL&gt; SELECT EMPNO,ENAME,SAL,COMM + 300 FROM SCOTT.EMP; EMPNO ENAME SAL COMM+300 ---------- ---------- ---------- ---------- 7369 SMITH 800 7499 ALLEN 1600 600 7521 WARD ...

    数据库设计及应用.doc

    参与逻辑运算:1、Null or true=ture 2、Null and false=false 3、其它情况结果为null(空值是一种状态,不是一个明确的值) 空值测试:is [not] null (例如 :where AGE is null ,不可写为where AGE = null) ...

    mysql面试题大全.docx

    • 索引字段上使用is null, is not null,可能导致索引失效。 • 左连接查询或者右连接查询查询关联的字段编码格式不一样,可能导致索引失效。 • mysql估计使用全表扫描要比使用索引快,则不使用索引。

    Mysql面试过关!(详解:索引+常用引擎+常见问题+sql调优)

    3.9 查询条件使用is null时正常走索引,使用is not null时,不走索引 3.10 查询条件使用not in、not exists时,如果是主键则走索引,如果是普通索引,则索引失效 3.11 当查询条件涉及到order by、l

    MySQL数据库经典面试题解析

    索引字段上使用is null, is not null,可能导致索引失效。 左连接查询或者右连接查询查询关联的字段编码格式不一样,可能导致索引失效。 mysql估计使用全表扫描要比使用索引快,则不使用索引。 索引不适合哪些场景 ...

    关系运算中的除操作怎么用SQL语句表示? 有套路的!(双 NOT EXISTS)

    简单介绍 我们有两个实体,分别是学生S、课程C,学生有... `cno` varchar(5) NOT NULL, `cname` varchar(20) DEFAULT NULL, `credit` int(11) DEFAULT NULL, PRIMARY KEY (`cno`) ) ENGINE=InnoDB DEFAULT CHARSET=u

    PLSQL程序优化和性能分析方法

    2.5.5 避免在索引列上使用IS NULL和IS NOT NULL 18 2.5.6 带通配符(%)的like语句 18 2.5.7 总是使用索引的第一个列 19 2.5.8 多个平等的索引 19 2.5.9 不明确的索引等级 19 2.5.10 自动选择索引 19 2.5.11 使用...

    MySQL面试经典100题(收藏版,附答案).doc

    索引字段上使用is null, is not null,可能导致索引失效。 左连接查询或者右连接查询查询关联的字段编码格式不一样,可能导致索引失效。 mysql估计使用全表扫描要比使用索引快,则不使用索引。 索引不适合哪些场景...

    人力资源管理系统数据库.doc

    " " " "籍贯 "char(5) "Not null " 表2 调动信息表 "字段名 "类型 "特殊属性 " "调动编号 "char(10) "主键 " "工号 "char(10) "外键 " "调动日期 "datetime "Not null " "调动情况 "char(50) "Not null " 表3 ...

    mysql数据库的基本操作语法

    name varchar(255) not null default ‘abc’, sex char null ) 上面的table加上了非空约束,也可以用alter来修改或增加非空约束 增加非空约束 alter table temp modify sex varchar(2) not null; 取消非空约束 ...

    【mysql面试题】100道MySQL数据库经典面试题解析

    索引字段上使用is null, is not null,可能导致索引失效。   左连接查询或者右连接查询查询关联的字段编码格式不一样,可能导致索引失效。   mysql估计使用全表扫描要比使用索引快,则不使用索引。  索引...

    mysql数据库操作

    注意null 与””不同 判断空:is null 判断非空:is not null 查询没有地址的同学; Select * from stu where address is null; e、优先级 小括号,not ,比较运算符 ,逻辑运算符,and比or优先级高,如果同时使用...

    SQL语句优化技术分析

     推荐方案:用其它相同功能的操作运算代替,如:a is not null 改为 a&gt;0 或a&gt;’’等。不允许字段为空,而用一个缺省值代替空值,如申请中状态字段不允许为空,缺省为申请。  3、&gt; 及 &lt; 操作符(大于或

    JAVA简单计算器程序设计

    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()-&gt;LoadIcon(IDR_MAINFRAME); // 初始化变量 this-&gt;m_dTempValue = 0; // 临时运算结果为0 this-&gt;m_dResult =...

Global site tag (gtag.js) - Google Analytics