我在下面有一个查询,而且sales_creditmemo表中的所有值都不存在于sales_order_item表中,所以很多“Totaal_inclusief BTW en excl credit”都是NULL。如何将c.base_grand_total设为0而不是NULL,因此总数为b.base_grand_total而不是NULL
SELECT a.order_id AS "Ordernummer", a.created_at AS "Orderdatum",
b.base_grand_total AS "Inclusief BTW", b.base_tax_amount AS "Berekende BTW",
c.base_grand_total AS "Credit-terugbetaald",
(b.base_grand_total - c.base_grand_total) AS "Totaal_inclusief BTW en excl
credit" FROM `sales_order_item` a
INNER JOIN sales_invoice b ON a.order_id = b.order_id
LEFT JOIN sales_creditmemo c ON a.order_id = c.order_id
WHERE a.created_at > '2017-01-01'
GROUP BY a.order_id
答案 0 :(得分:1)
使用coalesce
功能:
SELECT a.order_id AS "Ordernummer",
a.created_at AS "Orderdatum",
b.base_grand_total AS "Inclusief BTW",
b.base_tax_amount AS "Berekende BTW",
c.base_grand_total AS "Credit-terugbetaald",
(b.base_grand_total - coalesce(c.base_grand_total, 0)) AS "Totaal_inclusief BTW en excl
credit"
FROM `sales_order_item` a
INNER JOIN sales_invoice b ON a.order_id = b.order_id
LEFT JOIN sales_creditmemo c ON a.order_id = c.order_id
WHERE a.created_at > '2017-01-01'
GROUP BY a.order_id