gpt4 book ai didi

sql - 如果字段在 MySQL 中为空,则返回 0

转载 作者:IT老高 更新时间:2023-10-28 12:49:48 29 4
gpt4 key购买 nike

在 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)

来自文档:

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.

关于sql - 如果字段在 MySQL 中为空,则返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3997327/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com