我需要编写一个查询,返回满足特定条件的所有值的总和,但如果没有找到行,查询需要返回0,而不是null.例如:
tab
+---------------+-----+
| descr | num |
+---------------+-----+
| hello there | 5 |
| hi there | 10 |
| hello | 10 |
| hi there! | 15 |
+---------------+-----+
Run Code Online (Sandbox Code Playgroud)
这个查询:
SELECT sum(num) AS val FROM tab WHERE descr LIKE "%hello%";
Run Code Online (Sandbox Code Playgroud)
应该而且确实会回来15.然而:
SELECT sum(num) AS val FROM tab WHERE descr LIKE "%greetings%";
Run Code Online (Sandbox Code Playgroud)
应该返回0,但确实返回null.
有人可以解释一下这是否可能?