2017-07-02 40 views
0

我有一個查詢下面,並不是sales_order_item表中存在sales_creditmemo表中的所有值,所以很多「Totaal_inclusief BTW en excl credit」爲NULL。我怎樣才能把c.base_grand_total爲0,而不是空的,所以一共是b.base_grand_total而非NULLmysql空連接NULL時切換爲0

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 

回答

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 
+0

謝謝!工作正常! –

+0

很高興幫助:-) –