从这样的MySQL触发器开始:
delimiter #
create trigger comments_after_ins_trig after insert on comments
for each row
begin
insert into comment_types (comment, user_id) values (new.comment, new.user_id);
end#
假设我想将插入分成不同的表,例如,如果注释包含粗俗的单词(LOCATE('sh**', comment) > 0
),而不是我想插入名为vulgar_comments的表中的comment_types,如果注释包含单词“thanks”或“nice”插入nice_comments等。基本上我如何将表名插入到该触发器的变量中?
答案 0 :(得分:3)
尝试这个,我已经测试了这个,这可以在我的结束:
delimiter $$
drop trigger if exists `comments_after_ins_trig`$$
create trigger `comments_after_ins_trig` after insert on comments
for each row
begin
if(locate("sh**", new.comment) > 0) then
insert into vulgar_comments(comment, user_id) values (new.comment, new.user_id);
else
insert into comment_types (comment, user_id) values (new.comment, new.user_id);
end if;
end$$
delimiter ;