MySQL查询空字段或非空字段(is null和not null)
现在我们先来把test表中的一条记录的birth字段设置为空。
mysql>updatetestsett_birth=nullwheret_id=1;
QueryOK,1rowaffected(0.02sec)
Rowsmatched:1 Changed:1 Warnings:0
OK,执行成功!
设置一个字段值为空时的语法为:set<字段名>=NULL
说明一下,这里没有大小写的区分,可以是null,也可以是NULL。
下面看看结果:
mysql>select*fromtest;
+------+--------+----------------------------------+------------+
|t_id|t_name|t_password |t_birth |
+------+--------+----------------------------------+------------+
| 1|name1 |12345678901234567890123456789012|NULL |
| 2|name2 |12345678901234567890123456789012|2013-01-01|
+------+--------+----------------------------------+------------+
2rowsinset(0.00sec)
接下来分别查询一下字段t_birth值为空或不为空的记录:
mysql>select*fromtestwheret_birthisnull;
+------+--------+----------------------------------+---------+
|t_id|t_name|t_password |t_birth|
+------+--------+----------------------------------+---------+
| 1|name1 |12345678901234567890123456789012|NULL |
+------+--------+----------------------------------+---------+
1rowinset(0.00sec)
mysql>select*fromtestwheret_birthisnotnull;
+------+--------+----------------------------------+------------+
|t_id|t_name|t_password |t_birth |
+------+--------+----------------------------------+------------+
| 2|name2 |12345678901234567890123456789012|2013-01-01|
+------+--------+----------------------------------+------------+
1rowinset(0.00sec)
说明:
1、查询字段值为空的语法:where<字段名>isnull
2、查询字段值不为空的语法:where<字段名>isnotnull
关于MySQL查询空字段或非空字段(isnull和notnull),本文就介绍这么多,希望对大家有所帮助,谢谢!