Home > Database > Mysql Tutorial > 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
Release: 2023-08-26 20:05:09
forward
835 people have browsed it

如果没有找到值,如何让 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;
Copy after login

Now let us implement the above syntax in the following query.

mysql> SELECT IFNULL(SUM(NULL), 0) AS SUMOFTWO;
Copy after login

The following is the output of the above query, which returns 0.

+----------+
| SUMOFTWO |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)
Copy after login
Copy after login

This is the syntax of COALESCE.

mysql> SELECT COALESCE(SUM(NULL),0) as SUMOFTWO;
Copy after login

The following is the output using the SUM() function returning 0.

+----------+
| SUMOFTWO |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)
Copy after login
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template