Home > Database > Mysql Tutorial > body text

How can we apply COALESCE() function on data values ​​of MySQL table?

王林
Release: 2023-09-10 23:21:05
forward
906 people have browsed it

我们如何对 MySQL 表的数据值应用 COALESCE() 函数?

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!