INSERT INTO ...返回多列(PostgreSQL)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20508387/
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
INSERT INTO ... RETURNING multiple columns (PostgreSQL)
提问by itchy23
I've searched around for an answer and it seems definitive but I figured I would double check with the Stack Overflow community:
Here's what I'm trying to do:
我四处寻找答案,这似乎是确定的,但我想我会与 Stack Overflow 社区仔细核对:
这是我正在尝试做的事情:
INSERT INTO my_table VALUES (a, b, c)
RETURNING (SELECT x, y, z FROM x_table, y_table, z_table
WHERE xid = a AND yid = b AND zid = c)
I get an error telling me I can't return more than one column.
It works if I tell it SELECT x FROM x_table WHERE xid = a
.
我收到一条错误消息,告诉我我不能返回多于一列。
如果我告诉它它会起作用SELECT x FROM x_table WHERE xid = a
。
Is this at all possible in a single query as opposed to creating a seperate SELECT
query?
与创建单独的SELECT
查询相比,这在单个查询中是否完全可能?
I'm using PostgreSQL 8.3.
我正在使用 PostgreSQL 8.3。
采纳答案by itchy23
@corvinusz answer was wrong for 8.3 but gave me a great idea that worked so thanks!
@corvinusz 对 8.3 的回答是错误的,但给了我一个有效的好主意,谢谢!
INSERT INTO my_table VALUES (a, b, c)
RETURNING (SELECT x FROM x_table WHERE xid = a),
(SELECT y FROM y_table WHERE yid = b),
(SELECT z FROM z_table WHERE zid = c)
I have no idea why the way it's stated in the question is invalid but at least this works.
我不知道为什么它在问题中陈述的方式无效,但至少这是有效的。
回答by corvinusz
Try this.
试试这个。
with aaa as (
INSERT INTO my_table VALUES(a, b, c)
RETURNING a, b, c)
SELECT x, y, z FROM x_table, y_table, z_table
WHERE xid = (select a from aaa)
AND yid = (select b from aaa)
AND zid = (select c from aaa);
In 9.3 similar query works.
在 9.3 中类似的查询工作。