Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

mysql trigger with insert and update after insert on table

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...