遇到问题而没有得到几个小时的暗示。也许onyone可以帮助我。
使用以下查询显示Topsellers。因此,产品的状态(有效或无效)保存在b.Article_Status中(0 =无效,1 =有效)。
如何获取产品系列中没有活动产品的结果列表的产品。但是,如果订购旧产品(表order_items中也是如此)现在仍然处于非活动状态并且尚未订购活动产品,则仍然会显示该产品。
实际查询如下所示。已经资助了一个解决方案,该解决方案在实际有效产品订购一次时有效,但仍然存在上述案例的问题。
SELECT count( a.order_itemid ) AS numOrders, c.Product_ID, c.Product_Name, d.producer_name
FROM order_items a
LEFT OUTER JOIN product_article b ON b.Article_ID = a.order_itemid
LEFT OUTER JOIN product c ON b.Article_Productid = c.Product_ID
LEFT OUTER JOIN producer d ON c.Product_Producer = d.producer_id
GROUP BY c.Product_ID
ORDER BY `numOrders` DESC
答案 0 :(得分:0)
解决方案是一个WHERE EXISTS子查询
SELECT count( a.order_itemid ) AS numOrders, c.Product_ID, c.Product_Name, d.producer_name
FROM order_items a
LEFT OUTER JOIN product_article b ON b.Article_ID = a.order_itemid
LEFT OUTER JOIN product c ON b.Article_Productid = c.Product_ID
LEFT OUTER JOIN producer d ON c.Product_Producer = d.producer_id
WHERE EXISTS (SELECT * FROM product_article x WHERE c.Product_ID = x.Article_Productid AND x.Article_Status = 1)
GROUP BY c.Product_ID
ORDER BY `numOrders` DESC
LIMIT 5