使用SUM的mysql UPDATE子查询

时间:2014-12-11 15:19:11

标签: mysql sum subquery

你能指导我为什么以下查询不起作用吗?

感谢

UPDATE campaigns SET available='0' WHERE product_id IN (SELECT product_id, sum(stock_qty) AS qty FROM `sku_colors_sizes` GROUP BY product_id HAVING sum(stock_qty) < 1) 

1 个答案:

答案 0 :(得分:0)

您必须从子查询中仅选择一列以使用IN谓词对其进行比较,您可以对其进行修改,以便仅选择product_id,如下所示:

UPDATE campaigns 
SET available='0'
WHERE product_id IN (SELECT product_id -- <=====
                     FROM
                     (
                       SELECT product_id, sum(stock_qty) AS qty 
                       FROM sku_colors_sizes 
                       GROUP BY product_id 
                       HAVING sum(stock_qty) < 1
                     ) AS t)