<bdo id='NkfEG'></bdo><ul id='NkfEG'></ul>
      <legend id='NkfEG'><style id='NkfEG'><dir id='NkfEG'><q id='NkfEG'></q></dir></style></legend>
      <i id='NkfEG'><tr id='NkfEG'><dt id='NkfEG'><q id='NkfEG'><span id='NkfEG'><b id='NkfEG'><form id='NkfEG'><ins id='NkfEG'></ins><ul id='NkfEG'></ul><sub id='NkfEG'></sub></form><legend id='NkfEG'></legend><bdo id='NkfEG'><pre id='NkfEG'><center id='NkfEG'></center></pre></bdo></b><th id='NkfEG'></th></span></q></dt></tr></i><div id='NkfEG'><tfoot id='NkfEG'></tfoot><dl id='NkfEG'><fieldset id='NkfEG'></fieldset></dl></div>
    1. <tfoot id='NkfEG'></tfoot>

        <small id='NkfEG'></small><noframes id='NkfEG'>

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

        How do I get SUM function in MySQL to return #39;0#39; if no values are found?(如果找不到值,如何在 MySQL 中获取 SUM 函数以返回“0?)
        <legend id='eprKN'><style id='eprKN'><dir id='eprKN'><q id='eprKN'></q></dir></style></legend>

              <tbody id='eprKN'></tbody>

                • <bdo id='eprKN'></bdo><ul id='eprKN'></ul>

                  <tfoot id='eprKN'></tfoot>
                  <i id='eprKN'><tr id='eprKN'><dt id='eprKN'><q id='eprKN'><span id='eprKN'><b id='eprKN'><form id='eprKN'><ins id='eprKN'></ins><ul id='eprKN'></ul><sub id='eprKN'></sub></form><legend id='eprKN'></legend><bdo id='eprKN'><pre id='eprKN'><center id='eprKN'></center></pre></bdo></b><th id='eprKN'></th></span></q></dt></tr></i><div id='eprKN'><tfoot id='eprKN'></tfoot><dl id='eprKN'><fieldset id='eprKN'></fieldset></dl></div>
                • <small id='eprKN'></small><noframes id='eprKN'>

                  本文介绍了如果找不到值,如何在 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"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to delete duplicate records in mysql database?(如何删除mysql数据库中的重复记录?)
                  Python Pandas write to sql with NaN values(Python Pandas 使用 NaN 值写入 sql)
                  MySQL Insert amp; Joins(MySQL 插入 amp;加入)
                  MySQL concat() to create column names to be used in a query?(MySQL concat() 创建要在查询中使用的列名?)
                  NodeJS responded MySQL timezone is different when I fetch directly from MySQL(当我直接从 MySQL 获取时,NodeJS 响应 MySQL 时区不同)
                  WHERE datetime older than some time (eg. 15 minutes)(WHERE 日期时间早于某个时间(例如 15 分钟))
                        <tbody id='jp8WN'></tbody>

                      <small id='jp8WN'></small><noframes id='jp8WN'>

                        <tfoot id='jp8WN'></tfoot>

                          <i id='jp8WN'><tr id='jp8WN'><dt id='jp8WN'><q id='jp8WN'><span id='jp8WN'><b id='jp8WN'><form id='jp8WN'><ins id='jp8WN'></ins><ul id='jp8WN'></ul><sub id='jp8WN'></sub></form><legend id='jp8WN'></legend><bdo id='jp8WN'><pre id='jp8WN'><center id='jp8WN'></center></pre></bdo></b><th id='jp8WN'></th></span></q></dt></tr></i><div id='jp8WN'><tfoot id='jp8WN'></tfoot><dl id='jp8WN'><fieldset id='jp8WN'></fieldset></dl></div>
                            <bdo id='jp8WN'></bdo><ul id='jp8WN'></ul>
                          • <legend id='jp8WN'><style id='jp8WN'><dir id='jp8WN'><q id='jp8WN'></q></dir></style></legend>