SQL 如何将空表的 MAX() 视为 0 而不是 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15475059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 14:19:05  来源:igfitidea点击:

How to treat MAX() of an empty table as 0 instead of NULL

sqloracle

提问by Mariusz Jamro

I try to select max value from table

我尝试从表中选择最大值

SELECT MAX(cid) FROM itemconfiguration;

However when table itemconfigurationis empty the MAX(cid)statements is evaluated to NULLwhile i need a number. How to handle this and treat NULLas 0 ?

但是,当表itemconfiguration为空时,MAX(cid)语句被评估为,NULL而我需要一个数字。如何处理这个并将其NULL视为 0 ?

回答by RB.

Just use Coalesceor NVLto handle NULLs.

只需使用CoalesceNVL来处理 NULL。

The following code will return 0 if MAX(cid)is NULL

如果MAX(cid)为 NULL ,以下代码将返回 0

SELECT COALESCE(MAX(cid), 0)
FROM   itemconfiguration

回答by zibidyum

SELECT NVL(MAX(cid), 0) FROM itemconfiguration;

SELECT NVL(MAX(cid), 0) FROM itemconfiguration;

回答by mrm aadil

Can replace a number when max return null using ISNULL,

可以在 max 返回 null 时使用ISNULL替换数字,

ISNULL(MAX(cid),0) FROM itemconfiguration;