1

是否可以将多个 postgresql 查询复制到单个 csv 文件中?截至目前,我正在将单个查询复制到 csv 文件中

copy (select * from table1) to 'file.csv' with csv header;

现在我想一起复制 2 个查询结果。那可能吗 ?就像是,

copy (select * from table1,select name from table2) to 'file.csv' with csv header

有什么帮助吗?

4

1 回答 1

3

你可以:

copy (select 'table1' as table_id, * from table1
      UNION ALL
      select 'table2' as table_id, * from table2)
 to 'file.csv' with csv header

如果表具有相同的结构。

或者您可以:

copy (select field1, field2, null, null from table1
      UNION ALL
      select null,null, field3, field4 from table2)
 to 'file.csv' with csv header

如果表没有相同的结构。

问题是 -COPY ... FROM只能与表格一起使用(详见此处)。

所以你需要:CREATE TEMP TABLE tmp_copy_tblCOPY tmp_copy_tbl FROM然后INSERT ... SELECT ... FROM tmp_copy_tbl

于 2012-12-05T10:11:13.373 回答