仅替换表格中的 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)

更新日期:2019 年 9 月 30 日

105 次浏览

开启您的 职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.