不需要的查询结果

时间:2013-12-27 20:04:55

标签: mysql sql database left-join

我在下面使用此查询创建的系统上为已售出和未售出的商品执行报告,每个工作正常,即可获得已售出和未售出的商品。

SELECT p.product_id, p.product_brand_id, p.product_model_id, p.product_subcategory_id, 
       p.product_retail_price, p.product_wholesale_price,
       SUM(IFNULL(ps.product_quantity,0)) AS product_quantity_sold,
       SUM(IFNULL(ps.product_total_price,0)) AS total_price_sold,
       pb.brand_name, pm.model_name, psub.subcategory_name
 FROM product p
 LEFT JOIN product_sold ps ON p.product_id = ps.product_id
 LEFT JOIN sales s ON ps.product_sales_id = s.sales_id
 JOIN product_brand pb ON pb.brand_id = p.product_brand_id
 JOIN product_model pm ON pm.model_id = p.product_model_id
 JOIN product_subcategory psub ON psub.subcategory_id = p.product_subcategory_id
   WHERE ( s.sales_id IS NULL
   OR ( s.sales_approved = '1' 
      AND s.sales_approved_time > '$start_timestamp'  
      AND s.sales_approved_time < '$end_timestamp'
      )
   )
   AND pb.brand_name NOT LIKE 'X%'
 GROUP BY p.product_id 
 ORDER BY product_quantity_sold DESC, pb.brand_name ASC, pm.model_name ASC 

但我想要的是将结果限制为

如果物品已售出且库存为0,则在结果中显示。

如果商品有库存但尚未售出 - 请在结果中显示。

如果商品没有库存且没有销售 - 请勿显示。

我尝试使用此查询。

 SELECT p.product_id, p.product_brand_id, p.product_model_id, p.product_subcategory_id, p.product_retail_price, p.product_wholesale_price, SUM(IFNULL(ps.product_quantity,0)) AS product_quantity_sold,SUM(IFNULL(ps.product_total_price,0)) AS total_price_sold, SUM(IFNULL(pq.product_quantity,0)) AS total_stock, pb.brand_name, pm.model_name, psub.subcategory_name
 FROM product p
 LEFT JOIN product_sold ps ON p.product_id = ps.product_id
 LEFT JOIN sales s ON ps.product_sales_id = s.sales_id
 LEFT JOIN product_stock pq ON p.product_id = pq.product_id
 JOIN product_brand pb ON pb.brand_id = p.product_brand_id
 JOIN product_model pm ON pm.model_id = p.product_model_id
 JOIN product_subcategory psub ON psub.subcategory_id = p.product_subcategory_id
   WHERE ( s.sales_id IS NULL
        OR ( s.sales_approved = '1' 
           AND s.sales_approved_time > '$start_timestamp'  
           AND s.sales_approved_time < '$end_timestamp'
      )
   )
   AND pb.brand_name NOT LIKE 'X%' 
   AND ((pq.product_id IS NOT NULL OR SUM(pq.product_quantity) != 0)  AND (ps.product_quantity != 0))
 GROUP BY p.product_id 
 ORDER BY product_quantity_sold DESC, pb.brand_name ASC, pm.model_name ASC

但是失败了。

固定!!我正在使用这个查询


SELECT p.product_id, 
    p.product_brand_id, 
    p.product_model_id, 
    p.product_subcategory_id,
    p.product_retail_price, 
    p.product_wholesale_price, 
    SUM(IFNULL(ps.product_quantity,0)) AS product_quantity_sold,
    SUM(IFNULL(ps.product_total_price,0)) AS total_price_sold, 
    SUM(IFNULL(pq.product_quantity,0)) AS total_stock, 
    pb.brand_name, 
    pm.model_name, 
    psub.subcategory_name
FROM product p
LEFT JOIN product_sold ps ON p.product_id = ps.product_id
LEFT JOIN sales s ON ps.product_sales_id = s.sales_id
LEFT JOIN product_stock pq ON p.product_id = pq.product_id
JOIN product_brand pb ON pb.brand_id = p.product_brand_id
JOIN product_model pm ON pm.model_id = p.product_model_id
JOIN product_subcategory psub ON psub.subcategory_id = p.product_subcategory_id
WHERE p.product_brand_id = $brand_id AND p.product_model_id = $model_id
AND ( s.sales_id IS NULL
   OR ( s.sales_approved = '1' 
      AND s.sales_approved_time > '$start_timestamp'  
      AND s.sales_approved_time < '$end_timestamp'
      )
   )
  AND pb.brand_name NOT LIKE 'X%'
GROUP BY p.product_id 
HAVING SUM(IFNULL(pq.product_quantity,0)) > 0 OR SUM(IFNULL(ps.product_quantity,0)) > 0
ORDER BY product_quantity_sold DESC, pb.brand_name ASC, pm.model_name ASC

2 个答案:

答案 0 :(得分:1)

您需要使用HAVING子句来过滤具有聚合函数的结果

SELECT p.product_id,
p.product_brand_id,
p.product_model_id, 
p.product_subcategory_id, 
p.product_retail_price, 
p.product_wholesale_price, 
SUM(IFNULL(ps.product_quantity,0)) AS product_quantity_sold,
SUM(IFNULL(ps.product_total_price,0)) AS total_price_sold, 
SUM(IFNULL(pq.product_quantity,0)) AS total_stock,
pb.brand_name, pm.model_name,
psub.subcategory_name
 FROM product p
 LEFT JOIN product_sold ps ON p.product_id = ps.product_id
 LEFT JOIN sales s ON ps.product_sales_id = s.sales_id
 LEFT JOIN product_stock pq ON p.product_id = pq.product_id
 JOIN product_brand pb ON pb.brand_id = p.product_brand_id
 JOIN product_model pm ON pm.model_id = p.product_model_id
 JOIN product_subcategory psub ON psub.subcategory_id = p.product_subcategory_id

 WHERE ( s.sales_id IS NULL
        OR ( s.sales_approved = '1' 
           AND s.sales_approved_time > '$start_timestamp'  
           AND s.sales_approved_time < '$end_timestamp'
      )
   )
   AND pb.brand_name NOT LIKE 'X%' 
   GROUP BY p.product_id 
   HAVING  ((pq.product_id IS NOT NULL OR total_stock != 0) 
   AND (ps.product_quantity != 0))
 ORDER BY product_quantity_sold DESC, pb.brand_name ASC, pm.model_name ASC

答案 1 :(得分:1)

您想在having

之前添加以下order by子句
having SUM(IFNULL(pq.product_quantity,0)) > 0 or SUM(IFNULL(ps.product_quantity,0)) > 0

您可能希望将其添加到第一个版本。我并不完全遵循第二版中的逻辑。