- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements