MySQL查询空字段或非空字段(is null和not null)_MySQL

PHP中文网
Release: 2016-05-27 13:44:41
Original
2185 people have browsed it

MySQL查询空字段或非空字段(is null和not null)_MySQL

现在我们先来把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
Copy after login

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)
Copy after login

接下来分别查询一下字段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)
Copy after login
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)
Copy after login


说明:
1、查询字段值为空的语法:where <字段名> is null
2、查询字段值不为空的语法:where <字段名> is not null

以上就是MySQL查询空字段或非空字段(is null和not null)_MySQL的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!