mysql如果为null显示0,如果MySQL中的字段为null,则返回0

In MySQL, is there a way to set the "total" fields to zero if they are NULL?

Here is what I have:

SELECT uo.order_id, uo.order_total, uo.order_status,

(SELECT SUM(uop.price * uop.qty)

FROM uc_order_products uop

WHERE uo.order_id = uop.order_id

) AS products_subtotal,

(SELECT SUM(upr.amount)

FROM uc_payment_receipts upr

WHERE uo.order_id = upr.order_id

) AS payment_received,

(SELECT SUM(uoli.amount)

FROM uc_order_line_items uoli

WHERE uo.order_id = uoli.order_id

) AS line_item_subtotal

FROM uc_orders uo

WHERE uo.order_status NOT IN ("future", "canceled")

AND uo.uid = 4172;

The data comes out fine, except the NULL fields should be 0.

How can I return 0 for NULL in MySQL?

解决方案IFNULL(expr1, 0)

From the documentation:

If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值