是否可以将多个 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
有什么帮助吗?
是否可以将多个 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
有什么帮助吗?
你可以:
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_tbl
,COPY tmp_copy_tbl FROM
然后INSERT ... SELECT ... FROM tmp_copy_tbl