Mysql 使用on update触发器将数据从一个表移动到另一个表

Mysql 使用on update触发器将数据从一个表移动到另一个表,mysql,sql,triggers,Mysql,Sql,Triggers,我是DB开发的新手。请帮助我创建一个触发器,用于将数据从一个表移动到另一个表 我有两个表,其中一个表包含“Transaction Status”,我想从其中将有关事务状态更改的记录移动到另一个已完成事务的表中。因此,一个表中的值将被删除并插入到另一个表中 请在以下触发器中更正我: create trigger transaction_state after update on test_archive for each row begin insert into test_me(select *

我是DB开发的新手。请帮助我创建一个触发器,用于将数据从一个表移动到另一个表

我有两个表,其中一个表包含“Transaction Status”,我想从其中将有关事务状态更改的记录移动到另一个已完成事务的表中。因此,一个表中的值将被删除并插入到另一个表中

请在以下触发器中更正我:

create trigger transaction_state after update on test_archive for each row begin
insert into test_me(select * from test_archive where new.Transaction_status = 2);
delete from test_archive where new.Transaction_status = 2;
end;

为什么我觉得我在帮你做作业?当有人将一行更新为事务_Status=2时,您的触发器可能会移动所有行。因为您没有将新表加入到test_归档表,所以WHERE子句对于所有行都是真的

如果确实希望将事务状态为2的所有行从test_archive移动到test_me,那么请删除FOR EACH和对新表的引用

create trigger transaction_state after update on test_archive 
  begin 
    insert into test_me
        select * from test_archive where Transaction_status = 2; 
    delete from test_archive where Transaction_status = 2; 
  end;

4年后为我节省了很多时间:)…谢谢!