为了理解它,我们使用表'Employee'中具有Salary = NULL的ID = 5和6的数据,如下所示-
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; Rows matched: 1 Changed: 1 Warnings: 0 mysql> Update Employee set Salary = COALESCE(Salary,30000) where Id = 6; 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)