300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > oracle sum函数返回空 关于SQL语句中SUM函数返回NULL的解决办法

oracle sum函数返回空 关于SQL语句中SUM函数返回NULL的解决办法

时间:2021-02-25 17:48:54

相关推荐

oracle sum函数返回空 关于SQL语句中SUM函数返回NULL的解决办法

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

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

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

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

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

SQL Server / MS Access

1 SELECTProductName,UnitPrice*(UnitsInStock+ISNULL(UnitsOnOrder,0))

2 FROMProducts

Oracle

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

1 SELECTProductName,UnitPrice*(UnitsInStock+NVL(UnitsOnOrder,0))

2 FROMProducts

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 SELECTProductName,UnitPrice*(UnitsInStock+IFNULL(UnitsOnOrder,0))

2 FROMProducts

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

1 SELECTProductName,UnitPrice*(UnitsInStock+COALESCE(UnitsOnOrder,0))

2 FROMProducts

原文:/BluceLee/p/3844804.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。