postgresql “数组推送”的 Postgres 数组附加和数组长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11007800/
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
Postgres Array Append & Array Length for 'Array Push'
提问by mu is too short
What is the best way to add an element to an array when the size of the array is not provided?
当未提供数组的大小时,将元素添加到数组的最佳方法是什么?
With array_append
this is what I can think of:
随着array_append
这是我能想到的:
UPDATE table SET array = array_append((SELECT array FROM table WHERE ...), 'element') WHERE ...;
With array_length
this is what I can think of:
随着array_length
这是我能想到的:
UPDATE table SET array[array_length((SELECT array FROM table WHERE ...), 1)+1] = element;
回答by mu is too short
The simplest thing would be:
最简单的事情是:
update table
set array = array_append(array, 'element')
where ...
or perhaps use the ||
operator:
或者使用||
运算符:
update table
set array = array || 'element'
where ...
Both of those are equivalent to the more common set n = n + 11
for numbers.
这两个都相当于更常见set n = n + 11
的数字。