zoukankan      html  css  js  c++  java
  • 关于SQL语句中SUM函数返回NULL的解决办法

    SUM 是SQL语句中的标准求和函数,如果没有符合条件的记录,那么SUM函数会返回NULL。

    但多数情况下,我们希望如果没有符合条件记录的情况下,我们希望它返回0,而不是NULL,那么我们可以使用例如下面的方法来处理:

    SELECT COALESCE(SUM(name),0) FROM person WHERE id > 0

     

    行了,这下就不用费事去处理返回结果是否为NULL的情况了。

    COALESCE 函数的意思是返回参数列表中第一个为空的值,该方法允许传入多个参数,该函数也是SQL中的标准函数。

    然后查了查关于对于NULL值的判断。地址:http://www.w3schools.com/sql/sql_isnull.asp

    SQL Server / MS Access

    1 SELECT ProductName,UnitPrice*(UnitsInStock+ISNULL(UnitsOnOrder,0))
    2  FROM Products


    Oracle

    Oracle does not have an ISNULL() function. However, we can use the NVL() function to achieve the same result:

    1 SELECT ProductName,UnitPrice*(UnitsInStock+NVL(UnitsOnOrder,0))
    2  FROM Products

    MySQL

    MySQL does have an ISNULL() function. However, it works a little bit different from Microsoft's ISNULL() function.

    In MySQL we can use the IFNULL() function, like this:

    1 SELECT ProductName,UnitPrice*(UnitsInStock+IFNULL(UnitsOnOrder,0))
    2  FROM Products

    or we can use the COALESCE() function, like this:

    1 SELECT ProductName,UnitPrice*(UnitsInStock+COALESCE(UnitsOnOrder,0))
    2  FROM Products
  • 相关阅读:
    linux环境下的makefile文件的编写(zz)
    linux 中vim的退格键的使用问题
    Design Complier Synthesis Script Templet
    Synthesis Summary 逻辑综合总结
    .net加密
    timestamp (TransactSQL) 时间戳
    ADO.NET连接池
    ASP.NET Web数据控件
    高效的读取二进制数据
    GridView
  • 原文地址:https://www.cnblogs.com/BluceLee/p/3844804.html
Copyright © 2011-2022 走看看