为了理解它,我们使用表“Employee”中的数据,其中 ID = 5 和 6 的 Salary=NULL,如下 -
mysql> Select * from Employee;
+----+--------+--------+
| ID | Name | Salary |
+----+--------+--------+
| 1 | Gaurav | 50000 |
| 2 | Rahul | 20000 |
| 3 | Advik | 25000 |
| 4 | Aarav | 65000 |
| 5 | Ram | NULL |
| 6 | Mohan | NULL |
+----+--------+--------+
6 rows in set (0.00 sec)
登录后复制
现在,以下查询将使用 COALESCE() 函数以及 UPDATE 和 WHERE 子句将值放在 NULL 的位置。
mysql> Update Employee set Salary = COALESCE(Salary,20000) where Id = 5;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> Update Employee set Salary = COALESCE(Salary,30000) where Id = 6;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> Select * from Employee;
+----+--------+--------+
| ID | Name | Salary |
+----+--------+--------+
| 1 | Gaurav | 50000 |
| 2 | Rahul | 20000 |
| 3 | Advik | 25000 |
| 4 | Aarav | 65000 |
| 5 | Ram | 20000 |
| 6 | Mohan | 30000 |
+----+--------+--------+
6 rows in set (0.00 sec)
登录后复制
以上就是如何使用 MySQL COALESCE() 函数在列中的 NULL 位置插入值?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!