将数据插入另一个表后,MySQL插入触发器

时间:2018-12-19 17:31:20

标签: mysql database-trigger

我创建了一个表trans,在插入表结果后,我想在其中存储数据。当我运行触发器时,我反过来又得到了两个数据,似乎触发器的每一行都将完整的语句存储到db中。我该如何将插入语句末尾的触发器更改为结果,该触发器将语句存储到表trans中一次?

这是我的触发器...

   CREATE TRIGGER trans_trigger
   AFTER INSERT
   ON resultate FOR EACH ROW
   insert into trans(ID_trans, Spieltag, Heimteam, ID) 
SELECT     
     CASE
        WHEN @Heimteam = Heimteam THEN @ID_before
        ELSE NULL
    END AS ID_Final,
    Spieltag,
    @Heimteam:=Heimteam as Heimteam,
    @ID_before:=IDer as ID
FROM
    resultate
ORDER BY Heimteam, Spieltag;

1 个答案:

答案 0 :(得分:0)

我更改了查询。现在,仅插入行将存储在trans db中。

   Select a11.ID_Final, a11.Spieltag, a11.Heimteam,a11.ID from
(
SELECT     
     CASE
        WHEN @Heimteam = Heimteam THEN @ID_before
        ELSE NULL
    END AS ID_Final,
    Spieltag,
    @Heimteam:=Heimteam as Heimteam,
    @ID_before:=ID as ID
FROM
    resultate
ORDER BY Heimteam, Spieltag 
) as a11
  join
 (
Select ID from resultate 
)as a12 
on a11.ID = a12.ID where a12.ID = new.ID;