How to select sum -or- 0 if no records exist?

How about:

SELECT COALESCE(sum(num), 0) AS val FROM tab WHERE descr LIKE "%greetings%";

The COALESCE function basically says “return the first parameter, unless it’s null in which case return the second parameter” – It’s quite handy in these scenarios.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)