为什么在sql查询中需要使用xxx is null而不能使用xxx=null详解
来源:互联网 发布:hbuilder移动app源码 编辑:程序博客网 时间:2024/05/22 10:45
要查询某列值为null的结果集时,查询语句如下:
select * from tablename where fieldname is null
但如果你使用了
select * from tablename where fieldname = null
是查不到正确的结果的。那这是为什么呢?要清楚为什么,就需要了解 SQL 中的三值逻辑了。
--------TURE, FALSE , UNKNOWN---------
在其它的编程语言中,逻辑表达式只有两种,要么是true,要么就是false。
而在 SQL 中却存在第三个逻辑表达式UnKnown,这个是 SQL 中特有的。
从字面意思上我们可以解理该值的意思是:什么都不知道,即什么都不是。
一般情况下我们将任何值(包括NULL本身)与NULL做比较的时候,都会返回UnKnown。包括 null = null, null <> null 都会返回false!
而在查询表达式中(比如where与having中),UnKnown会视为false。所以我们就明白了为什么 select * from tablename where fieldname=null 查不到正确的结果的原因了。
-----------
需要注意的是,在分组子句与排序子句中,sql视null是相等的
即:
1,GROUP BY会把所有NULL值分到一组。
2,ORDER BY会把所有NULL值排列在一起。
原文地址:http://www.lmwlove.com/ac/ID853
0 0
- 为什么在sql查询中需要使用xxx is null而不能使用xxx=null详解
- 为什么在sql查询中需要使用xxx is null而不能使用xxx=null 详解
- 为什么在sql查询中需要使用xxx is null而不能使用xxx=null详解
- xxx.equals(null),xxx==null
- SQLServer XXX IS NOT NULL
- source is null for getProperty(null, "xxx")
- “is Null”或“is not null”不能使用索引
- xxx==null和xxx.equals(null)的区别
- XXX.equals(null)和XXX==null
- sql语句查询时is not null 的使用
- not in 不能使用在子查询结果为null
- hive中使用is null和is not null问题
- hive中使用is null和is not null问题
- mysql 查找没有插入数据的项不能使用 ***=Null 得用is null
- java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect
- java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect
- 远程mysql_java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect
- java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect
- Linux文件系统四(文件系统的创建流程与维护)
- navicat快捷键
- 一、基本输入输出
- 3d014期马后炮解太湖藏机图
- 析构函数定义为虚函数原因
- 为什么在sql查询中需要使用xxx is null而不能使用xxx=null详解
- map和hash_map的比较
- Java初学习 - 自动拆装箱的说明
- mysql load data infile一则
- C# ArrayList类的用法
- iOS开发 Error: CGImageProviderCreate: invalid image provider size
- TCP/IP_socket_in_C笔记
- Action Bank A High-Level Representation of Activity in Video实验
- Android布局文件属性值