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  >  Article  >  Database  >  How do I make the SUM function in MySQL return "0" if a value is not found?

How do I make the SUM function in MySQL return "0" if a value is not found?

WBOY
WBOYforward
2023-08-26 20:05:09804browse

如果没有找到值,如何让 MySQL 中的 SUM 函数返回“0”?

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete