How to replace 'Empty set' in a MySQL query?


To replace a record that doesn’t exist, use the COALESCE in MySQL. The COALESCE would help in substituting the NULL values. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Code varchar(20)
   -> );
Query OK, 0 rows affected (1.64 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('10');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('45');
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable values('78');
Query OK, 1 row affected (0.21 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------+
| Code |
+------+
| 10   |
| 45   |
| 78   |
+------+
3 rows in set (0.00 sec)

Here is the query to replace 'Empty set' in a MySQL query −

mysql> select COALESCE(MAX(Code), 'false') AS WhenReturnSetIsEmpty
   -> from DemoTable
   -> where Code='90';

This will produce the following output −

+----------------------+
| WhenReturnSetIsEmpty |
+----------------------+
| false                |
+----------------------+
1 row in set (0.00 sec)

Updated on: 13-Dec-2019

500 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements