在 postgresql 中输出多个 sql 查询的结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17008749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Outputting results from multiple sql queries in postgresql
提问by PS1
I have postgresql-9.2installed on my local machine (running windows 7) and I am also the administrator. I am using the Query Tool of pgAdmin IIIto query my database. My problem is as follows:
我在本地机器(运行 Windows 7)上安装了postgresql-9.2,我也是管理员。我正在使用pgAdmin III的查询工具来查询我的数据库。我的问题如下:
Say I have two tables Table_A
and Table_B
with different number of columns. Also, say I have following two very simple queries:
说我有两个表Table_A
,并Table_B
用不同的列数。另外,假设我有以下两个非常简单的查询:
select * from Table_A;
select * from Table_B;
I want to run both these queries and see the output from both of them together. I dont mind if I see the output in the GUI or in a file.
我想同时运行这两个查询并查看它们的输出。我不介意在 GUI 或文件中看到输出。
I also tried the copy command and outputting to a csv. But instead of appending to the file it overwrites it. So, I always end up with the results from query 2 only. The same thing happens with the GUI.
我还尝试了复制命令并输出到 csv。但是它不会附加到文件中,而是覆盖它。所以,我总是只得到查询 2 的结果。GUI 也会发生同样的事情。
It is really annoying to comment one query, run the another, output to two different files and then merge those two files together.
评论一个查询,运行另一个查询,输出到两个不同的文件,然后将这两个文件合并在一起真的很烦人。
回答by Ibrahim Dauda
This is not currently supported by PostgreSQL - from the docs (http://www.postgresql.org/docs/9.4/interactive/libpq-exec.html):
PostgreSQL 当前不支持此功能 - 来自文档 ( http://www.postgresql.org/docs/9.4/interactive/libpq-exec.html):
The command string can include multiple SQL commands (separated by semicolons). Multiple queries sent in a single PQexec call are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands included in the query string to divide it into multiple transactions. Note however that the returned PGresult structure describes only the result of the last command executed from the string. Should one of the commands fail, processing of the string stops with it and the returned PGresult describes the error condition.
命令字符串可以包含多个 SQL 命令(以分号分隔)。在单个 PQexec 调用中发送的多个查询在单个事务中处理,除非查询字符串中包含显式 BEGIN/COMMIT 命令以将其划分为多个事务。但是请注意,返回的 PGresult 结构仅描述从字符串执行的最后一个命令的结果。如果其中一个命令失败,字符串的处理将随之停止,返回的 PGresult 描述错误条件。
回答by Erwin Brandstetter
Your problem does not depend on the client.
您的问题不取决于客户。
Assuming all columns to be of type text
, try this query:
假设所有列都是 type text
,试试这个查询:
SELECT col_a AS col_ac, col_b AS col_bd
,NULL::text AS col_e, NULL::text AS col_f
FROM table_a
UNION ALL
SELECT col_c, col_d, col_e, col_f
FROM table_b;
Column names and data tapes are defined by the first branch of a UNION SELECT
. The rest has to fall in line.
列名和数据磁带由UNION SELECT
. 其余的必须排队。
回答by ra1
You can use UNION ALL, but you need to make sure each sub query has the same number of columns.
您可以使用 UNION ALL,但您需要确保每个子查询具有相同的列数。
SELECT 'a', 'b'
UNION ALL
SELECT 'c' ;
won't work.
不会工作。
SELECT 'a', 'b'
UNION ALL
SELECT 'c', 'd'
will work
将工作