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

编程入门 行业动态 更新时间:2024-10-28 00:21:32
本文介绍了如果MySQL中的字段为null,则返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在MySQL中,是否可以将总计"字段设置为零(如果它们为NULL)?

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

这就是我所拥有的:

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;

数据正常,除了NULL字段应为0.

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

如何在MySQL中为NULL返回0?

How can I return 0 for NULL in MySQL?

推荐答案

使用 IFNULL :

IFNULL(expr1, 0)

从文档中:

如果expr1不为NULL,则IFNULL()返回expr1;否则返回expr2. IFNULL()返回数字或字符串值,具体取决于使用它的上下文.

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.

更多推荐

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

本文发布于:2023-10-20 01:20:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1509406.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   MySQL   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!