How set 0 with MAX function when it is NULL? How set 0 with MAX function when it is NULL? mysql mysql

How set 0 with MAX function when it is NULL?


Well, as there is no date like 2014, you would expect null, because the maximum of nothing is actually not anyting.

But do this:

COALESCE(MAX(number),0)

Which means: get the first non-null thing from the next list, so if your max is null, it'll give you 0


COALESCE works, but IFNULL seems clearer to me.

IFNULL(MAX(number), 0)

If the first expression is not NULL, IFNULL() returns the expression itself, otherwise it returns the second parameter.IFNULL() returns a numeric or string value, depending on the context in which it is used.