[英]Show null results when left join don't find match
I have problem with join left. 我有加入左的问题。 It work fine but i don't see null results. 它工作正常,但我看不到空结果。 It connects three tables - categories, product_categories and order_products. 它连接三个表-类别,product_categories和order_products。
Query made by stack user look's like: 堆栈用户外观的查询如下:
SELECT
categories.name,categories.id,
SUM((orders_products.product_price_gross + orders_products.option_price)*(1 - (orders_products.rebate/100)) * orders_products.product_qty) as suma
FROM orders_products
LEFT JOIN product_categories
ON product_categories.product_id = orders_products.product_id
LEFT JOIN categories
ON product_categories.category_id = categories.id
GROUP BY categories.name, categories.id
ORDER BY suma DESC
I'm not sure how upgrade this query - I need to see what never been sold to show on shop statistics. 我不确定如何升级此查询-我需要查看从未售出的商品才能在商店统计信息中显示。
I hope You could help me. 我希望你能帮助我。
Kind regards Mark 亲切的问候马克
You want to see products that has not been sold? 您想查看尚未售出的产品吗? Your main table is the transaction table orders_products
. 您的主表是交易表orders_products
。 So any product not "ordered" will not show. 因此,任何未“订购”的产品都不会显示。 Interchange product_categories
and orders_products
to achieve what you want. 交换product_categories
和orders_products
以实现您想要的。 This will list all categories and null out the "suma" if JOIN statement cannot find the product_id
listed in the "order_products" table 这将列出所有类别,如果JOIN语句找不到“ order_products”表中列出的product_id
,则将“ suma”排除在外
SELECT
categories.name,
categories.id,
SUM((orders_products.product_price_gross + orders_products.option_price)*(1 - (orders_products.rebate/100)) * orders_products.product_qty) AS suma
FROM product_categories
LEFT JOIN orders_products
ON product_categories.product_id = orders_products.product_id
LEFT JOIN categories
ON product_categories.category_id = categories.id
GROUP BY categories.name, categories.id
ORDER BY suma DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.