Postgres在表之间复制数据

liv*_*v a 6 sql postgresql

我创建table1a,b,c,d包含数据的列. table2基本上是相同的,因为table1它有不同的列顺序+附加列,即a,e,d,b,c没有数据.

如何将数据复制table1table2注释列a是一个id,我希望数字保持不变.

这是我已经尝试过的:

insert into table2 select (a,d,b,c) from table1
Run Code Online (Sandbox Code Playgroud)

这导致了 column "a" is of type bigint but expression is of type record

insert into table2 (a,d,b,c) values(select a,d,b,c from table1)
Run Code Online (Sandbox Code Playgroud)

也没用 syntax error at or near "select"

insert into table2 (a,e,d,b,c) values(select a,NULL,d,b,c from table1)
Run Code Online (Sandbox Code Playgroud)

得到了错误: INSERT has more target columns than expressions

Rob*_*rco 15

指定要插入的列名,但values在定义select时不要使用.

insert into table2(a,d,b,c) select a, d, b, c  from table1
Run Code Online (Sandbox Code Playgroud)

  • 对于> 1mln行,从性能角度来看,这是一个很好的解决方案吗? (5认同)