Postgresql turn null into zero [duplicate]

2019-03-22 20:17发布

Possible Duplicate:
SELECT max(x) is returning null; how can I make it return 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?

3条回答
地球回转人心会变
2楼-- · 2019-03-22 20:42

Do either of these work?

  • select coalesce(max(foo),0) from bar
    
  • coalesce((select max(foo) from bar),0)
    
查看更多
虎瘦雄心在
3楼-- · 2019-03-22 20:51

Try:

SELECT coalesce(max(column), 0) myalias FROM mytable;
查看更多
Evening l夕情丶
4楼-- · 2019-03-22 20:58
select coalesce(max(column), 0) from mytable; 
查看更多
登录 后发表回答