在MySQL中,如何替换特定表的特定字段中的所有NULL值?
要替换特定表的特定字段中的所有NULL值,请使用具有IS NULL属性的UPDATE命令。语法如下:
UPDATE yourTableName SET yourColumnName=”yourValue’ WHERE yourColumnName IS NULL;
为了理解上述语法,让我们创建一个表。创建表的查询如下:
mysql> create table Employee_Information_Table-> (
-> Id int NOT NULL AUTO_INCREMENT,
-> Name varchar(20),
-> Salary int,
-> PRIMARY KEY(Id)
-> );
使用insert命令在表中插入一些记录。插入记录的查询如下:
mysql> insert into Employee_Information_Table(Name,Salary) values('John',NULL);mysql> insert into Employee_Information_Table(Name,Salary) values('Carol',NULL);
mysql> insert into Employee_Information_Table(Name,Salary) values('Bob',NULL);
mysql> insert into Employee_Information_Table(Name,Salary) values('David',NULL);
mysql> insert into Employee_Information_Table(Name,Salary) values('Robert',NULL);
mysql> insert into Employee_Information_Table(Name,Salary) values('Mike',NULL);
mysql> insert into Employee_Information_Table(Name,Salary) values('Sam',NULL);
使用select语句显示表中的所有记录。查询如下:
mysql> select *from Employee_Information_Table;
以下是输出:
+----+--------+--------+| Id | Name | Salary |
+----+--------+--------+
| 1 | John | NULL |
| 2 | Carol | NULL |
| 3 | Bob | NULL |
| 4 | David | NULL |
| 5 | Robert | NULL |
| 6 | Mike | NULL |
| 7 | Sam | NULL |
+----+--------+--------+
7 rows in set (0.00 sec)
这是将所有NULL值替换为特定表的特定字段的查询。查询如下:
mysql> update Employee_Information_Table-> set Salary=45500 where Salary IS NULL;
Rows matched: 7 Changed: 7 Warnings: 0
现在再次检查表记录。所有NULL值都已更新为某个值。以下是使用select语句列出表中所有记录的查询:
mysql> select *from Employee_Information_Table;
以下是输出:
+----+--------+--------+| Id | Name | Salary |
+----+--------+--------+
| 1 | John | 45500 |
| 2 | Carol | 45500 |
| 3 | Bob | 45500 |
| 4 | David | 45500 |
| 5 | Robert | 45500 |
| 6 | Mike | 45500 |
| 7 | Sam | 45500 |
+----+--------+--------+
7 rows in set (0.00 sec)
以上是 在MySQL中,如何替换特定表的特定字段中的所有NULL值? 的全部内容, 来源链接: utcz.com/z/316727.html