如果没有找到值,如何在 MySQL 中获取 SUM 函数以返回“0"?

How do I get SUM function in MySQL to return #39;0#39; if no values are found?(如果没有找到值,如何在 MySQL 中获取 SUM 函数以返回“0?)
本文介绍了如果没有找到值,如何在 MySQL 中获取 SUM 函数以返回“0"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在 MySQL 中有一个简单的函数:

Say I have a simple function in MySQL:

SELECT SUM(Column_1)
FROM Table
WHERE Column_2 = 'Test'

如果 Column_2 中没有条目包含文本Test",则此函数返回 NULL,而我希望它返回 0.

If no entries in Column_2 contain the text 'Test' then this function returns NULL, while I would like it to return 0.

我知道类似的问题在这里被问过几次,但我无法根据我的目的调整答案,所以我很感激能帮助我解决这个问题.

I'm aware that a similar question has been asked a few times here, but I haven't been able to adapt the answers to my purposes, so I'd be grateful for some help to get this sorted.

推荐答案

使用COALESCE 来避免这种结果.

Use COALESCE to avoid that outcome.

SELECT COALESCE(SUM(column),0)
FROM   table
WHERE  ...

要查看它的实际效果,请查看此 sql fiddle:http://www.sqlfiddle.com/#!2/d1542/3/0

To see it in action, please see this sql fiddle: http://www.sqlfiddle.com/#!2/d1542/3/0

更多信息:

给定三个表(一个包含所有数字,一个包含所有空值,一个包含混合):

Given three tables (one with all numbers, one with all nulls, and one with a mixture):

SQL 小提琴

MySQL 5.5.32 架构设置:

CREATE TABLE foo
(
  id    INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  val   INT
);

INSERT INTO foo (val) VALUES
(null),(1),(null),(2),(null),(3),(null),(4),(null),(5),(null),(6),(null);

CREATE TABLE bar
(
  id    INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  val   INT
);

INSERT INTO bar (val) VALUES
(1),(2),(3),(4),(5),(6);

CREATE TABLE baz
(
  id    INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  val   INT
);

INSERT INTO baz (val) VALUES
(null),(null),(null),(null),(null),(null);

查询 1:

SELECT  'foo'                   as table_name,
        'mixed null/non-null'   as description,
        21                      as expected_sum,
        COALESCE(SUM(val), 0)   as actual_sum
FROM    foo
UNION ALL

SELECT  'bar'                   as table_name,
        'all non-null'          as description,
        21                      as expected_sum,
        COALESCE(SUM(val), 0)   as actual_sum
FROM    bar
UNION ALL

SELECT  'baz'                   as table_name,
        'all null'              as description,
        0                       as expected_sum,
        COALESCE(SUM(val), 0)   as actual_sum
FROM    baz

结果:

| TABLE_NAME |         DESCRIPTION | EXPECTED_SUM | ACTUAL_SUM |
|------------|---------------------|--------------|------------|
|        foo | mixed null/non-null |           21 |         21 |
|        bar |        all non-null |           21 |         21 |
|        baz |            all null |            0 |          0 |

这篇关于如果没有找到值,如何在 MySQL 中获取 SUM 函数以返回“0"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)