現在我們先來把test表中的一條記錄的birth字段設置為空。
mysql> update test set t_birth=null where t_id=1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
OK,執行成功!
設置一個字段值為空時的語法為:set <字段名>=NULL
說明一下,這裡沒有大小寫的區分,可以是null,也可以是NULL。
下面看看結果:
mysql> select * from test;
+------+--------+----------------------------------+------------+
| t_id | t_name | t_password | t_birth |
+------+--------+----------------------------------+------------+
| 1 | name1 | 12345678901234567890123456789012 | NULL |
| 2 | name2 | 12345678901234567890123456789012 | 2013-01-01 |
+------+--------+----------------------------------+------------+
2 rows in set (0.00 sec)
接下來分別查詢一下字段t_birth值為空或不為空的記錄:
mysql> select * from test where t_birth is null;
+------+--------+----------------------------------+---------+
| t_id | t_name | t_password | t_birth |
+------+--------+----------------------------------+---------+
| 1 | name1 | 12345678901234567890123456789012 | NULL |
+------+--------+----------------------------------+---------+
1 row in set (0.00 sec)
mysql> select * from test where t_birth is not null;
+------+--------+----------------------------------+------------+
| t_id | t_name | t_password | t_birth |
+------+--------+----------------------------------+------------+
| 2 | name2 | 12345678901234567890123456789012 | 2013-01-01 |
+------+--------+----------------------------------+------------+
1 row in set (0.00 sec)
說明:
1、查詢字段值為空的語法:where <字段名> is null
2、查詢字段值不為空的語法:where <字段名> is not null
關於MySQL查詢空字段或非空字段(is null和not null),本文就介紹這麼多,希望對大家有所幫助,謝謝!