2017-07-02 87 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

很高兴帮助:-) –