2014-03-01 40 views
4

我創建了table1,其中有數據的列a,b,c,dtable2基本上與table1相同,它具有不同的列順序+附加列,即沒有數據的a,e,d,b,cPostgres在表格間複製數據

如何將table1中的數據複製到table2請注意,aid,我希望號碼保持不變。

這是我已經嘗試過:

insert into table2 select (a,d,b,c) from table1 

這導致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) 

沒有工作,要麼syntax error at or near "select"

insert into table2 (a,e,d,b,c) values(select a,NULL,d,b,c from table1) 

得到了錯誤:INSERT has more target columns than expressions

+0

[...'上的INSERT.'精細的手工](http://www.postgresql.org/docs/current/interactive/sql-insert.html) –

回答

13

指定要插入的列名,但在定義選擇時不要使用values

insert into table2(a,d,b,c) select a, d, b, c from table1 
+4

這是性能良好的解決方案在> 1毫米行的情況下的透視? – user253202