我有加入左边的问题。它工作正常,但我没有看到空结果。它连接三个表 - 类别,product_categories和order_products。
堆栈用户查询的查询如下:
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
我不确定如何升级此查询 - 我需要查看从未在商店统计信息中显示的内容。
我希望你能帮助我。
亲切的问候 标记
答案 0 :(得分:1)
您想看到尚未售出的产品吗?您的主表是事务表orders_products
。因此,任何未“订购”的产品都不会显示。交换product_categories
和orders_products
以实现您的目标。如果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