仅对MySQL表中的NULL值设置值

使用IFNULL检查NULL值,并使用SET命令设置一个值。让我们首先创建一个表-

mysql> create table DemoTable817(Value int);

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable817 values(10);
mysql> insert into DemoTable817 values(null);
mysql> insert into DemoTable817 values(20);
mysql> insert into DemoTable817 values(null);

使用select语句显示表中的所有记录-

mysql> select *from DemoTable817;

这将产生以下输出-

+-------+
| Value |
+-------+
| 10    |
| NULL  |
| 20    |
| NULL  |
+-------+
4 rows in set (0.00 sec)

以下是仅为NULL值设置值的查询-

mysql> update DemoTable817 set Value=IFNULL(Value,0);
Rows matched: 4 Changed: 2 Warnings: 0

让我们再次检查表记录-

mysql> select *from DemoTable817;

这将产生以下输出-

+-------+
| Value |
+-------+
| 10    |
| 0     |
| 20    |
| 0     |
+-------+
4 rows in set (0.00 sec)