Postgresql turn null into zero [duplicate]

2019-03-22 20:13发布

问题:

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?

回答1:

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


回答2:

Try:

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


回答3:

Do either of these work?

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