仅替换表格中的 NULL 值的 MySQL 查询是什么?
为此,您对 NULL 值可以使用 MySQL 中的属性 IS NULL。我们首先创建一个表——
mysql> create table DemoTable ( Name varchar(100) ); Query OK, 0 rows affected (0.53 sec)
使用插入命令在表中插入一些记录——
mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable values(null);
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values(null);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('Robert');
Query OK, 1 row affected (0.18 sec)使用 select 语句从表中显示所有记录——
mysql> select *from DemoTable;
这将产生以下输出——
+--------+ | Name | +--------+ | Robert | | NULL | | David | | NULL | | Robert | +--------+ 5 rows in set (0.00 sec)
以下是用于从表中替换 NULL 值的查询——
mysql> update DemoTable set Name=IF(Name IS NULL,'Please Enter a Name',Name); Query OK, 2 rows affected (0.09 sec) Rows matched: 5 Changed: 2 Warnings: 0
让我们再次检查表记录——
mysql> select *from DemoTable;
这将产生以下输出——
+---------------------+ | Name | +---------------------+ | Robert | | Please Enter a Name | | David | | Please Enter a Name | | Robert | +---------------------+ 5 rows in set (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP