SQL postgresql:选择返回数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5982174/
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
postgresql: select returning ARRAY
提问by Mayank
I have a table as:
我有一张桌子:
CREATE TABLE tbl_temp (id serial, friend_id int, name varchar(32));
I wish I could run the following SQL:
我希望我可以运行以下 SQL:
PREPARE x AS SELECT {,friend_id} FROM tbl_temp WHERE id = ANY();
EXECUTE x(33, ARRAY[1,2,3,4])
I basically looking for a statement that will return me an array of two ints first of which will be user input and second will be from table column like friend_id
.
我基本上在寻找一个语句,该语句将返回一个包含两个整数的数组,其中第一个是用户输入,第二个将来自表列,如friend_id
.
Is it really possible in PostgreSQL?
在 PostgreSQL 中真的有可能吗?
The results from SELECT ($1, friend_id) FROM tbl_temp;
SELECT ($1,friend_id) FROM tbl_temp; 的结果
EXECUTE x(44);
row
--------
(44,1)
(44,2)
(44,3)
(3 rows)
If I use PQgetvalue(PGres, 0, 0)
how will the result look like: {44,45}
or like (44,45)
?
如果我使用PQgetvalue(PGres, 0, 0)
结果如何:{44,45}
或像(44,45)
?
回答by mu is too short
I think you want to use the array constructor syntax:
我认为您想使用数组构造函数语法:
SELECT ARRAY[, friend_id] FROM tbl_temp WHERE id = ANY()
回答by David Chan
i'm not sure i understand what you want...
我不确定我明白你想要什么......
to return an array, do this.
要返回一个数组,请执行此操作。
SELECT (44, "friend_id") FROM "tbl_temp" WHERE id = ANY(ARRAY[1,2,3,4]);