0

我试图在表中插入新行之前更新同一个表。我想将具有相同 product_id 的所有先前行的状态设置为 0,然后插入状态为 1 的新行 ....请帮助。这是我写的mysql代码..

DROP TRIGGER IF EXISTS `priceStatusUpdate`//
CREATE TRIGGER `priceStatusUpdate` BEFORE INSERT ON `sale_price`
 FOR EACH ROW BEGIN
  UPDATE sale_price 
  SET status=0
  WHERE product_id=new.product_id;
END
//
4

2 回答 2

1

改用存储过程:

DELIMITER $$
CREATE PROCEDURE procedureName (IN p_product_id)
BEGIN
UPDATE sale_price SET status = 0 WHERE product_id = p_product_id;
INSERT INTO sale_price (product_id) VALUES (p_product_id);
END $$
DELIMITER ;

然后调用它

CALL procedureName(1);
于 2013-07-19T13:30:21.443 回答
0
DROP TRIGGER IF EXISTS `priceStatusUpdate`//
CREATE TRIGGER `priceStatusUpdate` BEFORE INSERT ON `sale_price`
FOR EACH ROW BEGIN
    SET NEW.status= 0;
END
于 2013-09-26T05:04:45.050 回答