微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数组 – 为Postgres整数数组添加值

我正在寻找帮助在Postgresql 9.5中为int []添加值10.

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

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子句来不更新整个表.

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

SELECT ARRAY[1,2],array_append(ARRAY[1,3);

结果:

array | array_append
-------+--------------
 {1,2} | {1,2,3}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐