mysql触发器比不同表中的graps值

时间:2018-03-14 15:23:50

标签: mysql triggers

我正在尝试创建一个为我的allocatese_counter添加+1的触发器

我有3张桌子:

dispense_table:

MasterID - containerID

master_table:

MasterID - customerID

container_table:

containerID - customerID - dispense_counter

我要做的是,在每次INSERT到我的allocatese_table之后,我想为我的allocatese_counter添加+1

WHERE 
master_table.customerID = container_table.customerID AND 
dispense_table.MasterID = master_table.MasterID AND 
dispense_table.containerID = container_table.containerID

因此,每当我在disposse_table中获得分配时,我想在我的container_table中添加+1,因为它是非常繁忙的表,并且需要一些时间来运行查询以在给定时间内分配。

所以

CREATE TRIGGER dispense_adding AFTER INSERT ON dispense_table
FOR EACH ROW BEGIN
  ????
END

1 个答案:

答案 0 :(得分:1)

dispense_table中新行中的列由特殊别名NEW.*引用

CREATE TRIGGER dispense_adding AFTER INSERT ON dispense_table
FOR EACH ROW BEGIN
  UPDATE master_table AS m JOIN container_table AS c ON m.customerID = c.customerID
  SET c.dispense_counter = c.dispense_counter + 1
  WHERE m.MasterID = NEW.MasterID AND c.containerID = NEW.containerID;
END