MySQL查询为NULL发生设置值
使用ISNULL查找NULL值,并使用MySQLUPDATE和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 );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable768(ClientName,ClientAge) values('John',23); mysql> insert into DemoTable768(ClientName,ClientAge) values(NULL,26); mysql> insert into DemoTable768(ClientName,ClientAge) values('Carol',28); mysql> insert into DemoTable768(ClientName,ClientAge) values(NULL,24);
使用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; 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)