Postgres数组附录“阵列推送”的阵列长度 [英] Postgres Array Append & Array Length for 'Array Push'
本文介绍了Postgres数组附录“阵列推送”的阵列长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不提供数组大小时向数组添加元素的最佳方法是什么?
With array_append
这就是我能想到的:
With array_append
this is what I can think of:
UPDATE table SET array = array_append((SELECT array FROM table WHERE ...), 'element') WHERE ...;
使用 array_length
of:
UPDATE table SET array[array_length((SELECT array FROM table WHERE ...), 1)+1] = element;
推荐答案
最简单的事情是:
update table
set array = array_append(array, 'element')
where ...
或使用 ||
运算符:
or perhaps use the ||
operator:
update table
set array = array || 'element'
where ...
这两个都等同于更常见的设置数字n = n + 11
。
Both of those are equivalent to the more common set n = n + 11
for numbers.
这篇关于Postgres数组附录“阵列推送”的阵列长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文