Return 0 when SUM is NULL

5

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
    
asked by anonymous 10.08.2018 / 15:27

2 answers

7

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
    
10.08.2018 / 15:40
5

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
    
10.08.2018 / 15:36