MySQL 中 NULL 值显示为 1

mysqlmysqli database更新于 2024/12/7 4:28:17

首先我们创建一个表 −

mysql> create table DemoTable1963
   (
   Counter int
   );
Query OK, 0 rows affected (0.00 sec)

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

mysql> insert into DemoTable1963 values(20);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1963 values(NULL);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1963 values(99);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1963 values(49);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1963 values(NULL);
Query OK, 1 row affected (0.00 sec)

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

mysql> select * from DemoTable1963;

这将产生以下输出 −

+---------+
| Counter |
+---------+
|      20 |
|    NULL |
|      99 |
|      49 |
|    NULL |
+---------+
5 rows in set (0.00 sec)

这是在 MySQL 中显示 NULL 值 1 的查询:

mysql> update DemoTable1963 set Counter=IFNULL(Counter,0)+1 where Counter IS NULL;
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2 Warnings: 0

让我们再次检查表记录 −

mysql> select * from DemoTable1963;

这将产生以下输出 −

+---------+
| Counter |
+---------+
|      20 |
|       1 |
|      99 |
|      49 |
|       1 |
+---------+
5 rows in set (0.00 sec)

相关文章


有用资源