向 Postgres 整数数组添加值
Adding value to Postgres integer array
我正在寻求帮助将值 10
添加到 PostgreSQL 9.5 中的 int[]
。
查看文档我应该可以使用这种格式来更新它,但它不起作用:
int[] + int push element onto array (add it to end of array)
我试过运行这个:
update table1 set integer_array = integer_array + 10::Integer.
它没有用,我得到了这个错误:
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
我觉得这与文档中关于如何执行此操作的格式相同。
使用array_append
函数在数组末尾追加一个元素:
UPDATE table1
SET integer_array = array_append(integer_array, 5);
5 是一个选择值,在您的情况下它是整数数据类型。您可能还需要一些 WHERE
子句来更新整个 table.
试试下面看看它是如何工作的:
SELECT ARRAY[1,2], array_append(ARRAY[1,2],3);
结果:
array | array_append
-------+--------------
{1,2} | {1,2,3}
我更喜欢这种方式:
UPDATE table1 SET integer_array = integer_array || '{10}';
您还可以通过单个查询添加多个值:
UPDATE table1 SET integer_array = integer_array || '{10, 11, 12}';
-- 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;
干杯
单身:
UPDATE table1
SET integer_array = array_append(integer_array, 3);
多个:
UPDATE table1
SET integer_array = array_cat(integer_array, ARRAY[4,5]);
https://www.postgresql.org/docs/9.1/functions-array.html#ARRAY-FUNCTIONS-TABLE
我正在寻求帮助将值 10
添加到 PostgreSQL 9.5 中的 int[]
。
查看文档我应该可以使用这种格式来更新它,但它不起作用:
int[] + int push element onto array (add it to end of array)
我试过运行这个:
update table1 set integer_array = integer_array + 10::Integer.
它没有用,我得到了这个错误:
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
我觉得这与文档中关于如何执行此操作的格式相同。
使用array_append
函数在数组末尾追加一个元素:
UPDATE table1
SET integer_array = array_append(integer_array, 5);
5 是一个选择值,在您的情况下它是整数数据类型。您可能还需要一些 WHERE
子句来更新整个 table.
试试下面看看它是如何工作的:
SELECT ARRAY[1,2], array_append(ARRAY[1,2],3);
结果:
array | array_append
-------+--------------
{1,2} | {1,2,3}
我更喜欢这种方式:
UPDATE table1 SET integer_array = integer_array || '{10}';
您还可以通过单个查询添加多个值:
UPDATE table1 SET integer_array = integer_array || '{10, 11, 12}';
-- 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;
干杯
单身:
UPDATE table1
SET integer_array = array_append(integer_array, 3);
多个:
UPDATE table1
SET integer_array = array_cat(integer_array, ARRAY[4,5]);
https://www.postgresql.org/docs/9.1/functions-array.html#ARRAY-FUNCTIONS-TABLE