为Postgres整数数组添加值

Kyl*_*e K 7 arrays postgresql postgresql-9.5

我在添加值寻求帮助10int[]PostgreSQL中9.5.

查看文档我应该能够使用这种格式来更新它,但它不起作用:

int[] + int   push element onto array (add it to end of array)
Run Code Online (Sandbox Code Playgroud)

我试过运行这个:

update table1 set integer_array = integer_array + 10::Integer. 
Run Code Online (Sandbox Code Playgroud)

它没有用,我收到了这个错误:

ERROR: operator does not exist: integer[] + integer
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 67
Run Code Online (Sandbox Code Playgroud)

我觉得这与文档中提供的有关如何执行此操作的格式相同.

Mar*_*fin 16

我更喜欢这种方式:

UPDATE table1 SET integer_array = integer_array || '{10}';
Run Code Online (Sandbox Code Playgroud)

您还可以使用单个查询添加多个值:

UPDATE table1 SET integer_array = integer_array || '{10, 11, 12}';
Run Code Online (Sandbox Code Playgroud)


Kam*_*ski 14

使用array_append函数在数组的末尾追加一个元素:

UPDATE table1
SET integer_array = array_append(integer_array, 5);
Run Code Online (Sandbox Code Playgroud)

5是一个选择值,在你的情况下它是一个整数数据类型.您可能还需要一些WHERE子句来更新整个表.

请尝试以下方法查看其工作原理:

SELECT ARRAY[1,2], array_append(ARRAY[1,2],3);
Run Code Online (Sandbox Code Playgroud)

结果:

 array | array_append
-------+--------------
 {1,2} | {1,2,3}
Run Code Online (Sandbox Code Playgroud)


小智 10

单身的:

UPDATE table1
SET integer_array = array_append(integer_array, 3);
Run Code Online (Sandbox Code Playgroud)

多种的:

UPDATE table1
SET integer_array = array_cat(integer_array, ARRAY[4,5]);
Run Code Online (Sandbox Code Playgroud)

https://www.postgresql.org/docs/9.1/functions-array.html#ARRAY-FUNCTIONS-TABLE


Fer*_*mes 5

-- Declaring the array

arrayName int8[];

-- Adding value 2206 to int array

arrayName := arrayName || 2206;

-- looping throught the array

FOREACH i IN ARRAY arrayName 
LOOP 

 RAISE NOTICE 'array value %', i;

END LOOP;
Run Code Online (Sandbox Code Playgroud)

干杯