為了理解它,我們使用表“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.92cms.cn其它相關文章!