将数据插入另一个表后的mysql insert触发器

lymgl2op  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(362)

我创建了一个表trans,希望在插入到表resultate之后在其中存储数据。当我运行触发器时,我在trans中返回了两个多数据,似乎每行触发器都将完整的语句存储到db中。如何将insert语句末尾的触发器更改为resultate,触发器将语句存储一次到table 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;
nsc4cvqm

nsc4cvqm1#

我更改了我的查询。现在,只有insert行将存储在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;

相关问题