插入2个表后的Mysql触发器

时间:2015-09-19 10:37:58

标签: mysql

我在创建在2个表中完成插入后执行的触发器时遇到了一些问题。

"insert into table_products values (NULL, '$product_name', '$product_description', '$image_path', '$brand_id', 1)";

"insert into table_product_categories values ($product_id, '$subcatty_id')";

执行这两个查询后,我想点击一个触发器:

UPDATE table_product_count SET count = count + 1 WHERE brand_id = NEW.brand_id && category_id = NEW.category_id;

问题是此时NEW.brand_id不可用。

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用join。说new是指table_product_categories

UPDATE table_product_count pc JOIN
       table_products p
       on p.product_id = pc.product_id and p.brand_id = pc.brand_id            
    SET pc.count = pc.count + 1
    WHERE p.product_id = NEW.product_id AND pc.category_id = NEW.category_id;