MySQL triggers cannot update rows in same table the trigger is assigned to. Suggested workaround?

前端 未结 5 1890
鱼传尺愫
鱼传尺愫 2020-11-28 15:11

MySQL doesn\'t currently support updating rows in the same table the trigger is assigned to since the call could become recursive. Does anyone have suggestions on a good wor

5条回答
  •  醉酒成梦
    2020-11-28 15:56

    You can actually up the rows in the same table as the trigger. The thread you linked to even has the solution.

    For example:

    TestTable ( id / lastmodified / random )
    
    create trigger insert_lastmod
    before insert on TestTable
    for each row
    set NEW.lastmodified = NOW();
    
    insert into TestTable ( `random` ) values ( 'Random' );
    
    select * from TestTable;
    +----+---------------------+---------------------+
    | id | lastmodified        | random              |
    +----+---------------------+---------------------+
    |  1 | 2010-12-22 14:15:23 | Random              |
    +----+---------------------+---------------------+
    

提交回复
热议问题