SQL之NULL值的幾種處理方式

一、建立測試表:mysql

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
;sql

二、查看數據:測試

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字符串。io

三、查詢name爲null的記錄:table

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

小結:能夠看到只有id in (5,6)的記錄name字段纔是真正的null。nio

四、查詢name爲''的記錄信息:數據

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

小結:能夠看到咱們輸入的以空格爲字符串的值都表現爲空字符串。而後\t字符串的缺沒有篩選出來。

五、查詢\t字符串的數據:

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

相關文章
相關標籤/搜索