When it does not find results it returns NULL
, how can I do for the search return me 0 when NULL
?
SELECT SUM(coluna1 + coluna2 + coluna3)
FROM table WHERE nome='funalo' AND MONTH(data) = 09 AND YEAR(data) = 2018
When it does not find results it returns NULL
, how can I do for the search return me 0 when NULL
?
SELECT SUM(coluna1 + coluna2 + coluna3)
FROM table WHERE nome='funalo' AND MONTH(data) = 09 AND YEAR(data) = 2018
Use IFNULL ().
Definition and use: The IFNULL () function allows you to return an alternate value if an expression is NULL. If the expression is NOT NULL, the IFNULL () function returns the expression. Source: W3schools
SELECT IFNULL(SUM(coluna1 + coluna2 + coluna3), 0)
FROM table
WHERE nome='funalo' AND MONTH(data) = 09 AND YEAR(data) = 2018
Just put a IFNULL
in your query:
SELECT IFNULL(SUM(coluna1 + coluna2 + coluna3), 0) AS Soma
FROM table
WHERE nome = 'funalo'
AND MONTH(data) = 09
AND YEAR(data) = 2018