让我们首先创建一个表-
mysql> create table DemoTable1963 ( Counter int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1963 values(20); mysql> insert into DemoTable1963 values(NULL); mysql> insert into DemoTable1963 values(99); mysql> insert into DemoTable1963 values(49); mysql> insert into DemoTable1963 values(NULL);
使用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; Rows matched: 2 Changed: 2 Warnings: 0
让我们再次检查表记录-
mysql> select * from DemoTable1963;
这将产生以下输出-
+---------+ | Counter | +---------+ | 20 | | 1 | | 99 | | 49 | | 1 | +---------+ 5 rows in set (0.00 sec)