用于设置 NULL 出现的 MySQL 查询
使用 IS NULL 查找 NULL 值,并用 MySQL UPDATE 和 SET 更新新值 -
update yourTableName set yourColumnName=yourValue where yourColumnName IS NULL;
我们首先创建一个表 -
mysql> create table DemoTable768 ( Clientid int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(100), ClientAge int ); Query OK, 0 rows affected (0.47 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable768(ClientName,ClientAge) values('John',23); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable768(ClientName,ClientAge) values(NULL,26); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable768(ClientName,ClientAge) values('Carol',28); Query OK, 1 row affected (0.53 sec) mysql> insert into DemoTable768(ClientName,ClientAge) values(NULL,24); Query OK, 1 row affected (0.16 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable768;
这将产生以下输出 -
+----------+------------+-----------+ | Clientid | ClientName | ClientAge | +----------+------------+-----------+ | 1 | John | 23 | | 2 | NULL | 26 | | 3 | Carol | 28 | | 4 | NULL | 24 | +----------+------------+-----------+ 4 rows in set (0.00 sec)
以下是设置 NULL 出现的查询 -
mysql> update DemoTable768 set ClientName='Chris' where ClientName IS NULL; Query OK, 2 rows affected (0.41 sec) Rows matched: 2 Changed: 2 Warnings: 0
让我们查看视图的说明 -
mysql> select *from DemoTable768;
这将产生以下输出 -
+----------+------------+-----------+ | Clientid | ClientName | ClientAge | +----------+------------+-----------+ | 1 | John | 23 | | 2 | Chris | 26 | | 3 | Carol | 28 | | 4 | Chris | 24 | +----------+------------+-----------+ 4 rows in set (0.00 sec)
广告