
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Returning a value even if there is no result in a MySQL query?
You can use IFNULL() function from MySQL to return a value even if there is not result. Let us create a table. Te query to create a table.
mysql> create table IfNullDemo −> ( −> Id int, −> Name varchar(100) −> ); Query OK, 0 rows affected (0.60 sec)
Insert some records in the table with the help of insert command. The query is as follows −
mysql> insert into IfNullDemo values(1,'John'); Query OK, 1 row affected (0.18 sec) mysql> insert into IfNullDemo values(200,'Sam'); Query OK, 1 row affected (0.21 sec) mysql> insert into IfNullDemo values(204,'Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into IfNullDemo values(510,'Johnson'); Query OK, 1 row affected (0.18 sec)
Display all records from the table with the help of select statement. The query is as follows −
mysql> select *from IfNullDemo;
The following is the output −
+------+---------+ | Id | Name | +------+---------+ | 1 | John | | 200 | Sam | | 204 | Carol | | 510 | Johnson | +------+---------+ 4 rows in set (0.00 sec)
Let us first return a value for TRUE condition −
The query is as follows −
mysql> select ifnull((select Id from IfNullDemo where Id = 200),'No Result Found') As ResultFound;
The following is the output −
+-------------+ | ResultFound | +-------------+ | 200 | +-------------+ 1 row in set (0.00 sec)
Now, let us return a value if there is no result using the IFNULL method. The query is as follows −
mysql> select ifnull((select Id from IfNullDemo where Id = 400),'No Result Found') As ResultFound;
The following is the output −
+-----------------+ | ResultFound | +-----------------+ | No Result Found | +-----------------+ 1 row in set (0.00 sec)
- Related Articles
- Generate the row count (serial number) of records after returning the result in MySQL query?
- MySQL query to return a string as a result of IF statement?
- Query returning no data in SAP Business One using Table Relationship
- What happens if MySQL query returns no rows?
- Set the result of a query to a variable in MySQL?
- Is there a default ORDER BY value in MySQL?
- How Sun burns even though there is no oxygen in the Galaxy?
- How to store Query Result in a variable using MySQL?
- How do I split a numerical query result in MySQL?
- Calculating percentage in a MySQL query and round off the result
- MySQL query to sum 3 different values in a column displaying total of each value in result set?
- Is there a way in MySQL to reverse a boolean field with a single query?
- Write a MySQL query to check if field exists and then return the result set?
- How can I set 0 if a query returns a null value in MySQL?
- How to assign the result of a MySQL query into a variable?

Advertisements