0

这是我以前的问题。我解决不了问题。我更新我的代码,我GROUP BY间接使用。但同样的问题显示。子查询返回重复值。其实我做了一个TRIGGER,所以我想如果show trigger,就很清楚了

--
-- Triggers `product_purchases`
--
DROP TRIGGER IF EXISTS `change_stock_when_purchases`;
DELIMITER //
CREATE TRIGGER `change_stock_when_purchases` AFTER INSERT ON `product_purchases`
 FOR EACH ROW BEGIN
  DECLARE X INTEGER;
  SET X = (SELECT product_size FROM product_stock WHERE product_id = NEW.product_id );
  IF NEW.product_size =X  THEN
    UPDATE
        product_stock as S1
        INNER JOIN
        (
        SELECT
            S.product_stock_id,I.quantity 
        FROM 
            product_stock AS S
        INNER JOIN
            product_purchase_item AS I 
        ON
            S.product_id=I.product_id AND S.product_size=I.product_size
        INNER JOIN
            product_purchases AS P
        ON
            I.product_purchase_item_id=P.product_purchase_item_id
        WHERE
            P.product_purchase_id=NEW.product_purchase_id
        GROUP BY 
            S.product_stock_id
        )
        AS M
    ON 
        S1.product_stock_id=M.product_stock_id
    SET
        S1.product_size_quantity=S1.product_size_quantity+M.quantity;
    ELSE
    INSERT INTO
        product_stock (product_id,product_size,product_size_quantity)
        SELECT
            product_id,product_size, quantity
        FROM
            product_purchase_item
        WHERE
            product_purchase_item_id=NEW.product_purchase_item_id;
  END IF;
END
//
DELIMITER ;

问题显示当它更新相同product_id的不同时product_sizeproduct_stock

4

1 回答 1

2

您的代码中最可疑的子查询是这个:

SET X = (SELECT product_size FROM product_stock WHERE product_id = NEW.product_id );

product_stock表中有重复项吗?这很容易确定:

select product_id
from product_stock
group by product_id
having count(*) > 1;

如果这样做,您可以选择其中之一:

SET X = (SELECT product_size FROM product_stock WHERE product_id = NEW.product_id limit 1);
于 2013-08-31T13:30:53.073 回答