为此,可以IF()
与UPDATE命令一起使用。让我们首先创建一个表-
mysql> create table DemoTable1601 -> ( -> FirstName varchar(20) , -> LastName varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1601 values('John','Doe'); mysql> insert into DemoTable1601 values('Adam',''); mysql> insert into DemoTable1601 values('David','Miller'); mysql> insert into DemoTable1601 values('Chris','');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1601;
这将产生以下输出-
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Doe | | Adam | | | David | Miller | | Chris | | +-----------+----------+ 4 rows in set (0.00 sec)
以下是将列更新为null或其值的查询-
mysql> update DemoTable1601 set LastName=if(LastName='',NULL,LastName); Rows matched: 4 Changed: 2 Warnings: 0
让我们再次检查表记录-
mysql> select * from DemoTable1601;
这将产生以下输出-
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | John | Doe | | Adam | NULL | | David | Miller | | Chris | NULL | +-----------+----------+ 4 rows in set (0.00 sec)