我的 Select SUM 查询返回 null.它应该返回 0 [英] My Select SUM query returns null. It should return 0

查看:45
本文介绍了我的 Select SUM 查询返回 null.它应该返回 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下查询总结客户余额:

I'm trying to sum up Customer balances using the following query:

select sum(balance) from mytable where customer = 'john' 

但是,如果客户没有余额(即 mytable 表中没有匹配的行),我的查询将返回 null 而不是 0.有什么问题?

However, if the customer has no balance (i.e. no matching rows in the mytable table), my query returns null and not 0. What's the problem?

推荐答案

试试这个:

select COALESCE(sum(balance),0) from mytable where customer = 'john' 

这应该可以完成工作.合并方法应该返回 0.

This should do the work. The coalesce method should return the 0.

这篇关于我的 Select SUM 查询返回 null.它应该返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆