2015-05-27 41 views
0

我有一個列在PostgreSQL表插入在PostrgeSQL陣列而不首先檢索陣列

modification_dates timestamp without time zone[] 

我需要能夠將日期添加到陣列。簡單的方法是獲取數組並添加元素,然後重寫它。

另一種方法是獲取數組,計算它並使用以下語法設置最後一個值。

UPDATE my_table SET timestamp [countOfArray] = newDate; 

有沒有辦法用一個SQL語句來做到這一點?

回答

3

您可以使用連接運算符或array_cat,按照該postgres documentation

UPDATE my_table SET timestamp = timestamp || newDate; 

UPDATE my_table SET timestamp = array_cat(timestamp, ARRAY[newDate]); 

其中的首選方法是前者。添加條件將像在任何其他更新查詢中一樣工作。

1

追加一個新值到陣列使用標準的SQL concatenation operator

update my_table 
    set modification_dates = modification_dates||current_timestamp 
where ...; 

在現有元件之間不能插入一個新元素。