在WHERE子句中使用SUM结果时出错

时间:2016-07-06 20:25:03

标签: mysql sum where

我使用操作SUM的结果遇到了一些麻烦,在where子句中称为“weight”。我很感激你的帮助。这是查询:

SELECT o.reference
     , SUM(od.product_weight * od.product_quantity) weight
     , SUM(od.total_price_tax_incl) total_paid_products
     , d.price
     , o.module
     , rw.id_range_weight
  FROM ps_orders o
     , ps_order_detail od
     , ps_product p
     , ps_supplier s
     , ps_delivery d
     , ps_address a
     , ps_state state
     , ps_range_weight rw 
 WHERE o.id_order = od.id_order 
   AND p.id_product = od.product_id 
   AND s.id_supplier = p.id_supplier 
   AND o.id_address_delivery = a.id_address 
   AND a.id_state = state.id_state 
   AND d.id_zone = state.id_zone 
   AND d.id_range_weight = rw.id_range_weight
   AND rw.delimiter1 <= weight 
   AND weight < rw.delimiter2
   AND 5 = rw.id_carrier
   AND p.id_supplier = 1 
   AND YEAR(o.date_upd) = 2016 
   AND MONTH(o.date_upd) = 5 
   AND o.current_state IN(2,3,4,5,15,16) 
   AND o.module IN('paypal','redsys','bankwire')
 GROUP 
    BY o.reference 
 ORDER 
    BY o.date_upd DESC

WHERE子句中的值“weight”没有SUM结果,值0总是...如果我替换一个完美的数字的权重,例如使用10而不是权重:

sql result

谢谢你们的时间!

2 个答案:

答案 0 :(得分:2)

对于具有和不在哪里的聚合函数使用

SELECT o.reference, Sum(od.product_weight * od.product_quantity) as weight,
SUM(od.total_price_tax_incl) as total_paid_products, d.price, o.module, rw.id_range_weight

FROM ps_orders o, ps_order_detail od, ps_product p, ps_supplier s, ps_delivery d, 
ps_address a, ps_state state, ps_range_weight rw 

WHERE o.id_order = od.id_order AND p.id_product = od.product_id AND 
s.id_supplier = p.id_supplier AND o.id_address_delivery = a.id_address 
AND a.id_state = state.id_state AND d.id_zone = state.id_zone 
AND d.id_range_weight = rw.id_range_weight

AND 5 = rw.id_carrier
AND p.id_supplier = 1 AND year(o.date_upd) = 2016 AND month(o.date_upd) = 5 
AND (o.current_state = 2 OR o.current_state = 3 OR o.current_state = 4 OR 
o.current_state = 5 OR o.current_state = 15 OR o.current_state = 16) 
AND (o.module = 'paypal' OR o.module = 'redsys' OR o.module = 'bankwire')

GROUP BY o.reference 

HAVING  rw.delimiter1 <= weight AND weight < rw.delimiter2

ORDER BY o.date_upd desc

答案 1 :(得分:0)

我找到了答案,感谢@scaisEdge为此

HAVING  rw.delimiter1 <= weight AND weight < rw.delimiter2

还要感谢@Strawberry让我知道SELECT子句中的非聚合列也应该包含在GROUP BY子句中。

以下是完整查询

SELECT o.reference
     , Sum(od.product_weight * od.product_quantity) AS weight
     , SUM(od.total_price_tax_incl) AS total_paid_products
     , d.price
     , o.module
     , rw.id_range_weight
  FROM ps_orders o
     , ps_order_detail od
     , ps_product p
     , ps_supplier s
     , ps_delivery d
     , ps_address a
     , ps_state state
     , ps_range_weight rw 
 WHERE o.id_order = od.id_order 
   AND p.id_product = od.product_id 
   AND s.id_supplier = p.id_supplier 
   AND o.id_address_delivery = a.id_address 
   AND a.id_state = state.id_state 
   AND d.id_zone = state.id_zone 
   AND d.id_range_weight = rw.id_range_weight
   AND 5 = rw.id_carrier
   AND p.id_supplier = 1 
   AND year(o.date_upd) = 2016 
   AND month(o.date_upd) = 5 
   AND (o.current_state = 2 
        OR o.current_state = 3 
        OR o.current_state = 4 
        OR o.current_state = 5 
        OR o.current_state = 15 
        OR o.current_state = 16) 
   AND (o.module = 'paypal' 
        OR o.module = 'redsys' 
        OR o.module = 'bankwire')
 GROUP 
    BY o.reference
     , rw.delimiter1
     , rw.delimiter2
HAVING rw.delimiter1 <= weight 
   AND weight < rw.delimiter2
 ORDER 
    BY o.date_upd desc

谢谢你们的时间。