在 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.
0
如何在 MySQL 中为 NULL 返回 0?
使用IFNULL:
IFNULL(expr1, 0)
从文档中:
如果 expr1 不为 NULL,则 IFNULL() 返回 expr1;否则返回 expr2。IFNULL() 返回一个数字或字符串值,具体取决于使用它的上下文。