我们先看一下效果,然后在解释,示例如下:

mysql> create table test5 (a int not null,b int,c varchar(10));
Query OK, 0 rows affected (0.01 sec) mysql> insert into test5 values (1,2,'a'),(3,null,'b'),(4,5,null);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from test5;
+---+------+------+
| a | b | c |
+---+------+------+
| 1 | 2 | a |
| 3 | NULL | b |
| 4 | 5 | NULL |
+---+------+------+
3 rows in set (0.00 sec)

上面我们创建了一个表test5,3个字段,a不能为空,b、c可以为空,插入了3条数据,睁大眼睛看效果了:

mysql> select * from test5 where b>0;
+---+------+------+
| a | b | c |
+---+------+------+
| 1 | 2 | a |
| 4 | 5 | NULL |
+---+------+------+
2 rows in set (0.00 sec) mysql> select * from test5 where b<=0;
Empty set (0.00 sec) mysql> select * from test5 where b=NULL;
Empty set (0.00 sec) mysql> select * from test5 t where t.b between 0 and 100;
+---+------+------+
| a | b | c |
+---+------+------+
| 1 | 2 | a |
| 4 | 5 | NULL |
+---+------+------+
2 rows in set (0.00 sec) mysql> select * from test5 where c like '%';
+---+------+------+
| a | b | c |
+---+------+------+
| 1 | 2 | a |
| 3 | NULL | b |
+---+------+------+
2 rows in set (0.00 sec) mysql> select * from test5 where c in ('a','b',NULL);
+---+------+------+
| a | b | c |
+---+------+------+
| 1 | 2 | a |
| 3 | NULL | b |
+---+------+------+
2 rows in set (0.00 sec) mysql> select * from test5 where c not in ('a','b',NULL);
Empty set (0.00 sec)

认真看一下上面的查询:

上面带有条件的查询,对字段b进行条件查询的,b的值为NULL的都没有出现。

对c字段进行like '%'查询、in、not查询,c中为NULL的记录始终没有查询出来。

between and查询,为空的记录也没有查询出来。

结论:查询运算符、like、between and、in、not in对NULL值查询不起效。

那NULL如何查询呢?继续向下看

IS NULL/IS NOT NULL(NULL值专用查询)

上面介绍的各种运算符对NULL值均不起效,mysql为我们提供了查询空值的语法:IS NULL、IS NOT NULL。

IS NULL(返回值为空的记录)

select 列名 from 表名 where 列 is null;

查询指定的列的值为NULL的记录。

如:

mysql> create table test7 (a int,b varchar(10));
Query OK, 0 rows affected (0.01 sec) mysql> insert into test7 (a,b) values (1,'a'),(null,'b'),(3,null),(null,null),(4,'c');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0 mysql> select * from test7;
+------+------+
| a | b |
+------+------+
| 1 | a |
| NULL | b |
| 3 | NULL |
| NULL | NULL |
| 4 | c |
+------+------+
5 rows in set (0.00 sec) mysql> select * from test7 t where t.a is null;
+------+------+
| a | b |
+------+------+
| NULL | b |
| NULL | NULL |
+------+------+
2 rows in set (0.00 sec) mysql> select * from test7 t where t.a is null or t.b is null;
+------+------+
| a | b |
+------+------+
| NULL | b |
| 3 | NULL |
| NULL | NULL |
+------+------+
3 rows in set (0.00 sec)

IS NULL(返回值不为空的记录)

select 列名 from 表名 where 列 is not null;

查询指定的列的值不为NULL的记录。

如:

mysql> select * from test7 t where t.a is not null;
+------+------+
| a | b |
+------+------+
| 1 | a |
| 3 | NULL |
| 4 | c |
+------+------+
3 rows in set (0.00 sec) mysql> select * from test7 t where t.a is not null and t.b is not null;
+------+------+
| a | b |
+------+------+
| 1 | a |
| 4 | c |
+------+------+
2 rows in set (0.00 sec)

关于mysql的null相关查询的一些坑的更多相关文章

  1. MySQL Transaction--事务相关查询

    MySQL支持的四种事务隔离级别 READ-UNCOMMITTED READ-COMMITTED REPEATABLE-READ SERIALIZABLE 查看全局事务隔离级别和会话事务隔离级别 SH ...

  2. Dapper+Mysql 使用LIKE模糊查询写法踩坑

    LIKE '%@Title%' 会解析成'%'@Title'%' 这里用拼接也是不行的'%'+@Title+'%' 只能用MySQL函数方法拼接 public dynamic GetListByFil ...

  3. 用Mysql进行emp、dept、salgrade表的相关查询操作

    初学者都会接触到三种表:emp.dept.salgrade表,进行练习各种语句操作再合适不过 但是,网上大多数的操作语句都是用oracle进行操作的,小编在学习mysql的时候,参考网上的书写遇到了不 ...

  4. mysql 个人博客应用的建表和相关查询

    一.建表 用户表tb_user create table if not exists tb_user( user_id int auto_increment, ) not null, user_pas ...

  5. mysql null 值查询问题

    我在开发公司内部的一个项目时遇到一个问题:select student_quality_id from STUDENT_QUALITY where mark_status=0 and batch_st ...

  6. MySQL库表状态查询

    一. 查看库的各链接状态 对于一个mysql连接或者一个线程,任何时刻都有一个状态,表示其当前正在做什么.一般使用show full processlist查看. +---------+------- ...

  7. PHP对MySQL数据库的相关操作

    一.Apache服务器的安装 <1>安装版(计算机相关专业所用软件---百度云链接下载)-直接install<2>非安装版(https://www.apachehaus.com ...

  8. MySQL Sending data导致查询很慢的问题详细分析【转载】

    转自http://blog.csdn.net/yunhua_lee/article/details/8573621 [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的时 ...

  9. Mysql的NULL和Empty String

    本文基于Mysql5.7版本的参考资料: https://dev.mysql.com/doc/refman/5.7/en/working-with-null.html https://dev.mysq ...

随机推荐

  1. linux echo命令颜色显示

    echo命令颜色显示: echo:      -n:  不换行.      -e:让转移符生效. \t(tab) \n (换行) 实例: $ echo -e "\033[34mabcd\03 ...

  2. CentOS 8 安装

    截止目前为止CentOS的最新版本为CentOS 8版本,接下来就介绍CentOS Linux 8.0.1905的安装过程 1. 安装CentOS 8 成功引导系统会显示如上图的界面: # 界面说明 ...

  3. Nginx 高级配置-状态页配置

    Nginx 高级配置-状态页配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 建议将nginx的监控状态的值通过zabbix或者Open-Falcon之类的监控工具来监控状态,并 ...

  4. WTL 9.0的变化 - atlcrack.h

    atlcrack.h中是一些对消息映射的简化,9.0版本中只增加了一个WM_MOUSEWHEEL的响应,而且要求windows vista. #if (_WIN32_WINNT >= 0x060 ...

  5. opencart 3伪静态seo url设置教程

    opencart 3已经为我们做好了url伪静态功能,我们只要做一些简单的设置就能实现seo url,首先开启伪静态功能,看看文件.htaccess事发后有存在如下规则,如果没有需要先添加下面的代码 ...

  6. pyinstaller安装及使用

    pyinstaller使用 将.py文件转换成无需源码的.exe可执行文件 下载 1.打开cmd直接输入pip install pyinstaller即可下载 2.如第一种方法无法下载,打开pyins ...

  7. Layer获取iframe的dom元素及调用iframe页的js方法

    1. 父页面点击第一个按钮触发,获取子页面中的body元素,调用子页面中定义的js方法 yes : function(index,layero){ //获取iframe的body元素 var body ...

  8. .Net反射-TypeDescriptor

    .Net中提供了两种方式访问类型的元数据:System.Reflection命名空间中提供的反射API和TypeDescriptor类.反射适用于所有类型的常规机制,它为类型返回的信息是不可扩展的,因 ...

  9. Angle Beats Gym - 102361A(计算几何)

    Angle Beats \[ Time Limit: 4000 ms \quad Memory Limit: 1048576 kB \] 题意 给出 \(n\) 个初始点以及 \(q\) 次询问,每次 ...

  10. 洛谷 p3870 开关 线段树模板

    这两天学了很长时间于是做了一道水题 我就用了模板,就连任何优化都没有 就AC了,复杂度也很爆炸10个点1500多毫秒 这个题就是把lazy[]改成记录下修改的次数,每次修改的时候mod 2,因为反过来 ...