我有3个名为opproduct,opcategory和opproduct_to_category的表.opproduct表有一个after insert和一个after delete触发器。这些触发器修改类别表内容列,但它通过opproduct_to_category表获取category_id。触发器的声明大致相同。更新后运行良好,但插入后不修改类别表。 谢谢你的回答!
DROP TRIGGER IF EXISTS opproduct_AINS;
DELIMITER $$
CREATE TRIGGER `opproduct_AINS` AFTER INSERT ON `opproduct`
FOR EACH ROW BEGIN
DECLARE _category_id INT;
IF new.`status`='1' THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory
SET content = content+1
WHERE category_id=_category_id;
END IF;
END$$
DELIMITER ;
DROP TRIGGER IF EXISTS opproduct_AUPD;
DELIMITER $$
CREATE TRIGGER `opproduct_AUPD` AFTER UPDATE ON `opproduct`
FOR EACH ROW BEGIN
DECLARE _category_id INT;
IF NEW.`status`<> OLD.`status` AND NEW.`status`=1 THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory SET content = content+1
WHERE category_id=_category_id;
ELSEIF NEW.`status`<> OLD.`status` AND NEW.`status`=0 THEN
SELECT category_id INTO _category_id
FROM opproduct_to_category
WHERE product_id = NEW.product_id;
UPDATE opcategory SET content = content-1
WHERE category_id=_category_id;
END IF;
END$$
DELIMITER ;