SQL Postgresql 将空值变为零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7452501/
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 12:09:55  来源:igfitidea点击:

Postgresql turn null into zero

sqlpostgresqlnullmax

提问by maverick

Possible Duplicate:
SELECT max(x) is returning null; how can I make it return 0?

可能重复:
SELECT max(x) 返回空值;我怎样才能让它返回0?

When I execute

当我执行

select max(column) from mytable;

and my table has no rows, it returns null. How can I amend this select statement so it will return zero?

我的表没有行,它返回空。如何修改此选择语句使其返回零?

回答by Mark Byers

select coalesce(max(column), 0) from mytable; 

回答by Phil Sandler

Try:

尝试:

SELECT coalesce(max(column), 0) myalias FROM mytable;

回答by vol7ron

Do either of these work?

这两个都行吗?

  • select coalesce(max(foo),0) from bar
    
  • coalesce((select max(foo) from bar),0)
    
  • select coalesce(max(foo),0) from bar
    
  • coalesce((select max(foo) from bar),0)