If we want to apply the COALESCE() function on the data values of the MySQL table, then we need to use the column name as the parameter of the function. If there is a NULL value in the first column, it checks the next column and so on until it finds a non-NULL value. We demonstrate the above concept using data from the 'employee' table as shown below:
mysql> Select * from employee; +----+------------+-------------+-----------+ | Id | First_Name | Middle_Name | Last_Name | +----+------------+-------------+-----------+ | 1 | Advik | NULL | Jhamb | | 2 | Rahul | Kumar | Sharma | | 3 | Raman | Singh | Rajput | | 4 | Krishan | Kumar | Khurana | | 5 | Sachin | Ramesh | Tendulkar | | 6 | NULL | Kumar | Gaurav | | 7 | NULL | Singh | Parmar | +----+------------+-------------+-----------+ 7 rows in set (0.00 sec) mysql> Select COALESCE(First_Name, Middle_Name,Last_Name)AS Name FROM Employee; +---------+ | Name | +---------+ | Advik | | Rahul | | Raman | | Krishan | | Sachin | | Kumar | | Singh | +---------+ 7 rows in set (0.03 sec)
In the above example, the COALESCE() function has three parameters and the above query returns the names from First_Name, Middle_Name and Last_Name , and if First_Name is NULL, returns the value of Middle_Name. Then for Last_Name, if First_Name and Middle_Name are NULL, return the value of Last_Name. If First_Name, Middle_Name and Last_Name are all NULL, NULL is returned.
The above is the detailed content of How can we apply COALESCE() function on data values of MySQL table?. For more information, please follow other related articles on the PHP Chinese website!