I need to write a query that returns the sum of all values that meet a certain criteria, but the query needs to return 0 if no rows are found, rather than null. For example:
tab
+---------------+-----+
| descr | num |
+---------------+-----+
| hello there | 5 |
| hi there | 10 |
| hello | 10 |
| hi there! | 15 |
+---------------+-----+
This query:
SELECT sum(num) AS val FROM tab WHERE descr LIKE "%hello%";
should, and does, return 15
. However:
SELECT sum(num) AS val FROM tab WHERE descr LIKE "%greetings%";
should return 0
, but does return null
.
Can someone explain if this is possible?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…