2016-11-16 50 views
0

我正在尋找幫助將值10添加到PostgreSQL 9.5中的int[]。在文檔爲Postgres整數數組添加值

尋找我應該能夠使用這種格式來更新它,但它不工作:

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 

我覺得這是像在文件中呈現的格式如何執行此操作的指導。

回答

2

使用array_append函數來在一個數組的末尾附加的元件:

UPDATE table1 
SET integer_array = array_append(integer_array, 5); 

5是一個選擇的值,這是你的情況的整數數據類型的。您可能需要一些WHERE子句以及不更新整個表。

嘗試下面來看看它是如何工作的:

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

結果:

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

感謝那些偉大的工作。一旦我可以接受這個答案,我會的。 –

+0

很高興能提供幫助:-) –

+0

我們可以在Java Spring Boot中做到這一點嗎? –