我试图通过以下查询总结客户余额:
select sum(balance) from mytable where customer = 'john'
但是,如果客户没有余额(即mytable
表中没有匹配的行),我的查询将返回null,而不是0.有什么问题吗?
我试图通过以下查询总结客户余额:
select sum(balance) from mytable where customer = 'john'
但是,如果客户没有余额(即mytable
表中没有匹配的行),我的查询将返回null,而不是0.有什么问题吗?
试试这个:
select COALESCE(sum(balance),0) from mytable where customer = 'john'
这应该能行.coalesce方法应返回0.