将数组连接到现有的postgresql数组

时间:2015-01-26 19:33:59

标签: arrays postgresql

我有一个postgresql数据库,每行都有一个现有的整数数组。我想用新数组连接每一行的现有数组。我按照文档(http://www.postgresql.org/docs/9.3/static/functions-array.html)尝试了以下内容,但这些都不起作用:

db=> update useradds set deals=array_append(deals, {4, 5});
ERROR:  syntax error at or near "{"
LINE 1: update useradds set deals=array_append(deals, {4, 5});
                                                      ^
db=> update useradds set deals=array_cat(deals,  {6,7});
ERROR:  syntax error at or near "{"
LINE 1: update useradds set deals=array_cat(deals,  {6,7});
                                                    ^
db=> update useradds set deals=array_cat(deals, {6,7});
ERROR:  syntax error at or near "{"
LINE 1: update useradds set deals=array_cat(deals, {6,7});
                                                   ^

db=> update useradds set deals=array_cat(deals,{6,7} );
ERROR:  syntax error at or near "{"
LINE 1: update useradds set deals=array_cat(deals,{6,7} );
                                                  ^
db=> update useradds set deals=array_cat(deals,{6} );
ERROR:  syntax error at or near "{"
LINE 1: update useradds set deals=array_cat(deals,{6} );
                                                  ^
db=> update useradds set deals=deals||{6,7};
ERROR:  syntax error at or near "{"
LINE 1: update useradds set deals=deals||{6,7};
                                         ^
db=> update useradds set deals = deals || {6,7};
ERROR:  syntax error at or near "{"

我正在运行v.9.3.5。这些电话有什么问题?谢谢。

1 个答案:

答案 0 :(得分:1)

您无法输入数组{6,7}。有两种可能性:

SELECT ARRAY[6,7];
SELECT '{6,7}'::int[]; -- **literal** casted as int array

如果可以加入两个数组,则必须使用array_cat函数或||运算符:

postgres=# SELECT array_cat(ARRAY[1,2], ARRAY[3,4]);
 array_cat 
-----------
 {1,2,3,4}
(1 row)

postgres=# SELECT ARRAY[1,2] || ARRAY[3,4];
 ?column?  
-----------
 {1,2,3,4}
(1 row)

http://www.postgresql.org/docs/9.4/static/arrays.html