SELECTIFNULL(SUM(NULL),0)ASSUMOFTWO;The following is the output of the above query, which returns 0. +----------+|SUMOFTWO|+----------+|0|+----------+1rowinset(0.00sec)This is COALESCE syntax"/> SELECTIFNULL(SUM(NULL),0)ASSUMOFTWO;The following is the output of the above query, which returns 0. +----------+|SUMOFTWO|+----------+|0|+----------+1rowinset(0.00sec)This is COALESCE syntax">
Home >Database >Mysql Tutorial >How do I make the SUM function in MySQL return '0' if a value is not found?
To return Sum as "0" if no value is found, use the IFNULL or COALESCE command.
The following is the syntax of IFNULL.
SELECT IFNULL(SUM(NULL), 0) AS aliasName;
Now let us 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)
This is the syntax of COALESCE.
mysql> SELECT COALESCE(SUM(NULL),0) as SUMOFTWO;
The following is the output using the SUM() function returning 0.
+----------+ | SUMOFTWO | +----------+ | 0 | +----------+ 1 row in set (0.00 sec)
The above is the detailed content of How do I make the SUM function in MySQL return '0' if a value is not found?. For more information, please follow other related articles on the PHP Chinese website!