select
COALESCE(sum(case when order_state='1' then paid_amount else 0 end),0) errSumMoney,
COALESCE(sum(case when order_state='2' then paid_amount else 0 end),0) sucSumMoney,
count(case when order_state='2' then id else 0 end) sucCountNum
from order_info
where TO_DAYS(create_date) = TO_DAYS('2020-12-13')
在Mysql做统计时候经常会用SUM求和的函数,但是这个函数就是在求和的时候在没有符合where条件的时候就会返回null值。这个null如果在代码里面不处理就会报空指针异常问题。但是按照一般业务需求,没有求和的值时候结果应该是0。那么就需要一个给它赋值一个0就好了。Mysql提供了函数coalesce。coalesce()解释:返回参数中的第一个非空表达式(从左向右依次类推)。
COALESCE("字段", 0) 给字段一个默认值