SQL之NULL值的几种处理方式

1、创建测试表:

drop table if exists tab_null_operator;
create table tab_null_operator as
select 1 as id,'chavin' as name union all
select 2 as id,'nope' as name union all
select 3 as id,'' as name union all
select 4 as id,'' as name union all
select 5 as id,null as name union all
select 6 as id,null as name union all
select 7 as id,' ' as name union all
select 8 as id,' ' as name union all
select 9 as id,'    ' as name union all
select 10 as id,'    ' as name
;

2、查看数据:

mysql> select * from tab_null_operator;
+----+--------+
| id | name   |
+----+--------+
|  1 | chavin |
|  2 | nope   |
|  3 |        |
|  4 |        |
|  5 | NULL   |
|  6 | NULL   |
|  7 |        |
|  8 |        |
|  9 |        |
| 10 |        |
+----+--------+
10 rows in set (0.00 sec)

小结:从结果我们可以看出,id in (5,6)的两个值是null,id in (1,2)的是字符串,id in (3,4,7,8,9,10)从结果看均是空字符串,实际id in (3,4)是空字符串,id in (7,8)是单个空格字符串,id in (9,10)是\t字符串。

3、查询name为null的记录:

mysql> select * from tab_null_operator where name is null;
+----+------+
| id | name |
+----+------+
|  5 | NULL |
|  6 | NULL |
+----+------+
2 rows in set (0.00 sec)

小结:可以看到只有id in (5,6)的记录name字段才是真正的null。

4、查询name为''的记录信息:

mysql> select * from tab_null_operator where name = '';
+----+------+
| id | name |
+----+------+
|  3 |      |
|  4 |      |
|  7 |      |
|  8 |      |
+----+------+
4 rows in set (0.00 sec)

小结:可以看到我们输入的以空格为字符串的值都表现为空字符串。然后\t字符串的缺没有筛选出来。

5、查询\t字符串的数据:

mysql> select * from tab_null_operator where name = '   ';
+----+------+
| id | name |
+----+------+
|  9 |      |
| 10 |      |
+----+------+
2 rows in set (0.00 sec)

posted @ 2018-11-29 16:39  ChavinKing  阅读(4266)  评论(0编辑  收藏  举报