本文介绍了在表上插入后插入和更新的mysql触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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)
);
我必须在 Test2 表 Test_No 上插入后更新和插入 Test1 Test_No 都有共同的字段 WRO.
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);
如果 test2 表的 wro no 与 test1 不匹配,则应在 test1 表中触发插入查询
if wro no of test2 table not match with test1 then insert query should be fired in test1 table
推荐答案
希望我能正确理解你.
如果WRO
表中不存在新行的WRO
字段,Test2
表上的以下触发器将向Test1
表中插入新行>Test1.
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
这篇关于在表上插入后插入和更新的mysql触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!