【发布时间】:2018-10-06 04:46:40
【问题描述】:
''正在使用 postgresql 9.5。
对于一个id,我可以查询如下:
select a,b from mytable where id = '1234' order by a,b;
第一季度。
我在一个 txt 文件中有 50 个感兴趣的唯一 ID。如何一次性对所有这 50 个 id 运行 ^ 查询?
(mytable 中总共有大约 100000 条记录)
第二季度。
如果可能的话,我还想将结果递归地保存到不同的文本文件中。
我可以做 1 个文件
COPY "select a,b from mytable where id = '1234' order by a,b;" TO STDOUT CSVHEADER > '/tmp/1234.csv'
参考:psql - save results of command to a file
Save PL/pgSQL output from PostgreSQL to a CSV file
额外信息:
我正在从本地机器查询。
postgres 安装在 AWS 上。 (我有读取数据库的权限)。
【问题讨论】:
-
之类的?
select a,b from mytable where id in (select id from table_created_with_50_other_ids) order by a,b; -
好的,谢谢。这听起来是一个很好的起点。我必须创建一个新表并将这 50 个 ID 加载到其中并按照您的提示进行操作。
标签: database postgresql loops