為了理解它,我們使用表格「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 位置插入值?的詳細內容。更多資訊請關注PHP中文網其他相關文章!