简体   繁体   English

在MySQL触发器中将整个表从一个表复制到另一个表

[英]Copy entire row from one table to another in MYSQL trigger

I have two tables with identical schemas, I want to copy the newly inserted record from table1 to table2 in a trigger, my code is below and works, but only copies the id field 我有两个具有相同架构的表,我想在触发器中将新插入的记录从table1复制到table2,我的代码在下面并且可以工作,但是只复制id字段

BEGIN
   insert into Table2 (id) values (new.id);
END

However the table many columns, is there a way to copy the entire record into the table2 without specifying all the columns and values, like below 但是表中有很多列,有一种方法可以将整个记录复制到table2中,而无需指定所有列和值,如下所示

BEGIN
   insert into Table2 SELECT * FROM NEW;
END

Thanks 谢谢

Your second SQL is fine but you have to do one query first for using the star in your select into : 您的第二个SQL很好,但是您必须先执行一次查询才能在选择中使用星号到:

SET IDENTITY_INSERT NEW ON

And then : 接着 :

BEGIN
   insert into Table2 SELECT * FROM NEW;
END

And dont forget to put it OFF at the end 并且不要忘记最后将其关闭

EDIT 1 编辑1

If it's not working, maybe try to put some priority on your sql : 如果它不起作用,也许尝试在sql上设置一些优先级:

INSERT INTO `Table2` (SELECT * FROM `NEW`);

EDIT 2 编辑2

If it's still not working, maybe you could try to create the table with all the element directly : 如果仍然无法使用,也许您可​​以尝试直接使用所有元素创建表:

CREATE TABLE Table2 [AS] SELECT * FROM NEW;

这有效

  insert into Table2 SELECT * FROM Table1 where id=new.id;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MYSQL:如何将整行从一个表复制到 mysql 中的另一个表,第二个表有一个额外的列? - MYSQL: How to copy an entire row from one table to another in mysql with the second table having one extra column? 如何在mysql中将整个行从一个表复制到另一个表,而第二个表具有修改后的字段 - How to copy an entire row from one table to another in mysql with the second table having a modified field mysql-将整个行从一个表复制到另一个表,并在一个查询中添加带条件的count(*) - mysql - copy entire row to from one table to another and add count(*) with conditions in one query MySQL:将整行从一个复制到另一个并删除原始行 - MySQL: Copy entire row from one to another and delete original Mysql触发器,将所有行从一个表复制到另一个表中 - Mysql Trigger, Copy All Rows From One Table Into Another Table MySQL将colmn数据从一个表复制到另一行 - MySQL copy colmn data from one table to another row MySQL将具有多个NOT IN条件的行从一个表复制到另一个表 - MySQL copy row from one table to another with multiple NOT IN criteria MYSQL触发器更新复制整行 - MYSQL Trigger Update Copy Entire Row MYSQL查询将值插入一行并将数据从另一个表复制到另一行 - MYSQL Query to INSERT INTO a value into one row and copy data from another table into another row MySQL-将具有不同行值的行列值从一个表复制到另一张表(2次) - Mysql - Copy row colum values from one table to another table (2 times) with different row values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM