mysql trigger with insert and update after insert on table

匿名 (未验证) 提交于 2019-12-03 01:27:01

问题:

create table Test1 ( WRO varchar(10), Test_No varchar(10), TestDate datetime );  insert into Test1(WRO,TestDate) values('T0001',now());  create table Test2 ( WRO varchar(10), Test_No varchar(10), Test2Date datetime, TestAmount varchar(10) ); 

I have to update and Insert on Test1 Test_No after insert on Test2 table Test_No both have common field WRO.

insert into Test2 values('DSK','400',now(),3000); insert into Test2 values('T0001','200',now(),3000); 

if wro no of test2 table not match with test1 then insert query should be fired in test1 table

回答1:

I hope that I understood you properly.

The following trigger on Test2 table will insert new row into Test1 table if WRO field of new row doesn't exist in Test1.

 CREATE TRIGGER `myTrigger` AFTER INSERT ON `Test2`  FOR EACH ROW BEGIN     if not exists (select 1 from Test1 where WRO = new.WRO) then          insert into Test1 (WRO, Test_No) values (new.WRO, new.Test_No);     end if;     END 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!