【发布时间】:2014-06-14 18:11:41
【问题描述】:
我正在使用 mysql,当有条件时,我需要在从表中插入时触发。
要在 Table1 上插入的信息是用户元数据,当有新用户注册时,它会插入 8 个新行。我希望在 metadata_key 为 'first name'
时触发触发器我的问题是如果将这个条件放在触发器中,这个行永远不会插入,所以信息丢失并且触发器永远不会触发。
这些是我的桌子:
+---------------------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
有什么建议吗?
【问题讨论】:
-
不应该是
if(new.metadata_key='first_name') then而不是new.meta_key -
谢谢,其实是meta_key和meta_value,我在例子里写错了,但是在代码里没问题
标签: mysql triggers phpmyadmin