This tutorial explains how to use the MySQL COALESCE function with syntax and examples.
The MySQL COALESCE function returns the first non-null expression in the list.
The syntax for the COALESCE function in MySQL is:
COALESCE( expression1, expression2, ... expression_n )
The expressions to test for non-null values.
The COALESCE function can be used in the following versions of MySQL:
Let's look at some MySQL COALESCE function examples and explore how to use the COALESCE function in MySQL.
For example:
mysql> SELECT COALESCE(null, null, null, 'A', 'B');
Output: 'A'
mysql> SELECT COALESCE('A', 'B', null, 'C', 'D');
Output: 'A'
mysql> SELECT COALESCE(null, 1, 2, 3, null, 4);
Output: 1
mysql> SELECT COALESCE(null, 'AODBA.com', 'mysite.com');
Output: 'AODBA.com'
mysql> SELECT COALESCE(null, null, null, null, null);
Output: NULL