
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can I insert a value in a column at the place of NULL using MySQL COALESCE() function?
To understand it, we are using the data from the table ‘Employee’ having Salary=NULL for ID = 5 and 6, as follows −
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)
Now, the following queries will use COALESCE() function along with UPDATE and WHERE clause to put values at the place of 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)
- Related Articles
- How can I use IFNULL() function at the place of COALESCE() function in MySQL?
- How do I insert a NULL value in MySQL?
- How to convert MySQL null to 0 using COALESCE() function?
- How can I customize value, instead of NULL, of a row by using MySQL IF() function?
- Place a specific value for NULL values in a MySQL column
- Insert NULL value into INT column in MySQL?
- How can we use INSERT() function to insert a new string into the value of a column of MySQL table?
- What happens when I insert the value ‘NULL’ in an AUTO_INCREMENT MySQL column?
- How can we apply COALESCE() function on a MySQL table’s data value?
- How to insert NULL keyword as a value in a character type column of MySQL table having NOT NULL constraint?
- Insert default into not null column if value is null in MySQL?
- How to insert DATE into a MySQL column value using Java?
- How can I set 0 if a query returns a null value in MySQL?
- How can I use MySQL INTERVAL() function with a column of a table?
- How can MySQL COALESCE() function be used with MySQL SUM() function to customize the output?

Advertisements