MySQL插入日期值取决于另一个日期值 - 触发器

时间:2013-09-30 11:29:32

标签: mysql date triggers

我有一张时间计划表:例如:一个月,三个月,六个月 我想将我的用户与此表链接

我想创建一个触发器,根据开始日期和计划表

更新我的终结日期
> CREATE TRIGGER `sum_fin`  BEFORE INSERT ON `planificare_leg`  FOR EACH
> ROW  BEGIN
>     IF NEW.`data_final` IS NULL THEN
>         SET NEW.data_final = 
> (SELECT  DATE_ADD(`data_start`,INTERVAL `valoare_durata` `fel_durata` ) as data_fin
> FROM v_users_planificare
> where codtimp = NEW.codtimp and user_id = NEW.user_id) ;
>     END IF; 
>     END;

valoare_durata是一个包含装载次数的整数字段。例如:对于计划3个月,valoare_durata = 3和fel_durata = MONTH

sql返回此错误:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fel_durata ) as data_fin FROM v_users_planificare where codtimp = NEW.codtimp an' at line 6

我创建了一个测试,看看会发生什么:

  

更新planificare_leg l加入v_users_planificare计划   plan.user_id = l.user_id和plan.codtimp = l.codtimp set activ = 1   其中DATE_ADD(data_start,INTERVAL 1 MONTH)< = NOW()和data_start> = NOW()

作品

  

更新planificare_leg l加入v_users_planificare计划   plan.user_id = l.user_id和plan.codtimp = l.codtimp set activ = 1   其中DATE_ADD(data_start,INTERVAL valoare_durata fel_durata)< =   NOW()和data_start> = NOW()

不起作用

1 个答案:

答案 0 :(得分:0)

啊,错过了一个paren

SET NEW.data_final = (SELECT  DATE_ADD(`data_start`,INTERVAL `valoare_durata` `fel_durata` )) as data_fin FROM v_users_planificare
                                                                                            ^