如果MySQL中的字段为null,则返回0
在MySQL中,是否可以将“总计”字段设置为零(如果它们为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
。
如何在MySQL中为NULL返回0?
回答:
使用IFNULL:
IFNULL(expr1, 0)
从文档中:
如果expr1不为NULL,则IFNULL()返回expr1; 否则返回expr2。IFNULL()返回数字或字符串值,具体取决于使用它的上下文。
以上是 如果MySQL中的字段为null,则返回0 的全部内容, 来源链接: utcz.com/qa/436350.html