在插入不插入行时触发条件

时间:2014-06-14 18:11:42

标签: mysql triggers phpmyadmin

我正在使用mysql,我需要在出现条件时从表格中触发。

将要在Table1上插入的信息是用户元数据,当有新用户注册时,它会插入8个新行。我想在metadata_key '

时触发触发器

我的问题是,如果将此条件置于触发器中,此行永远不会插入,因此信息丢失且触发器永远不会触发

这些是我的表格:

+---------------------Table1---------------+ Table which fires the trigger AFTER INSERT 
+ user_id | meta_key     | meta_value      +
+ -----------------------------------------+
+      1  | first_name   | luke            +
+      1  | second_name  | skywalker       +
+------------------------------------------+

' 
+-----------------Table2------------------+ Where I need to INSERT
+ post_author | post_content | post_title +
+ ----------------------------------------+
+        0    | hub         | luke        +
+-----------------------------------------+

代码

DROP TRIGGER IF EXISTS  `post` ;

CREATE DEFINER =  `anagomez`@`localhost` TRIGGER `post` AFTER INSERT ON  `table1` 

FOR EACH ROW INSERT 

if(new.meta_key='first_name') then

insert into table2(post_author,post_content,post_title) values ('0','hub',new.meta_value);

end if

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您的触发器几乎是正确的,但是有一个小问题,它应该看起来像

delimiter //

CREATE DEFINER =  `anagomez`@`localhost` TRIGGER `post` AFTER INSERT ON  `table1` 

FOR EACH ROW  
BEGIN

   if(new.meta_key='first_name') then

     insert into table2(post_author,post_content,post_title) values ('0','hub',new.meta_value);

   end if;
end; //
delimiter ;

我添加了分隔符,问题是您错过了BEGIN,而是添加了INSERT

相关问题