
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get SUM Function in MySQL to Return 0 if No Values are Found
To return Sum as ?0' if no values are found, use IFNULL or COALESCE commands.
The following is the syntax for IFNULL.
SELECT IFNULL(SUM(NULL), 0) AS aliasName;
Let us now implement the above syntax in the following query.
mysql> SELECT IFNULL(SUM(NULL), 0) AS SUMOFTWO;
The following is the output of the above query, which returns 0.
+----------+ | SUMOFTWO | +----------+ | 0 | +----------+ 1 row in set (0.00 sec)
Here is the syntax for COALESCE.
mysql> SELECT COALESCE(SUM(NULL),0) as SUMOFTWO;
The following is the output that returns 0 using the SUM() function.
+----------+ | SUMOFTWO | +----------+ | 0 | +----------+ 1 row in set (0.00 sec)
Advertisements