简体   繁体   English

当空切换到0时,mysql离开连接为空

[英]mysql left join NULL when empty switch to 0

I have a query below and not all values in the sales_creditmemo table exist in the sales_order_item table, so a lot of the "Totaal_inclusief BTW en excl credit" is NULL. 我在下面有一个查询,而不是sales_creditmemo表中的所有值都存在于sales_order_item表中,因此很多“ Totaal_inclusief BTW en excl信用”为NULL。 How can i take the c.base_grand_total as 0 instead of NULL, so the total is b.base_grand_total instead of 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

By using the coalesce function: 通过使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM