我有一個integer[]
列,我想更新此列,但避免在數組中有重複的條目。更新PostgreSQL數組列 - 避免重複值
例如:第一個[123]
是值。 下一次我要添加234
。這裏array_append()
工作正常。但它不應該允許再次添加123
。
所以我的問題:只有當該項目還沒有出現在數組中時,我如何將一個值附加到數組中。
我有一個integer[]
列,我想更新此列,但避免在數組中有重複的條目。更新PostgreSQL數組列 - 避免重複值
例如:第一個[123]
是值。 下一次我要添加234
。這裏array_append()
工作正常。但它不應該允許再次添加123
。
所以我的問題:只有當該項目還沒有出現在數組中時,我如何將一個值附加到數組中。
僞代碼:
UPDATE "my_table"
SET "int_array" = array_append("int_array", :element_to_insert)
WHERE :some_filters
AND :element_to_insert <> ALL ("int_array")
更多的可能性:
http://www.postgresql.org/docs/9.3/static/functions-array.html http://www.postgresql.org/docs/9.3/static/functions-comparisons.html
對於整型數組,你可以use the intarray
extension's uniq
function:
CREATE EXTENSION intarray;
UPDATE thetable SET thecol = uniq(array_append(thecol, 32)) WHERE ...
Dosnt這種沖洗和更新只新條目每次?它是否保留舊的條目? – Qstacker
當數組不包含要附加的元素時,此更新* only *。數組包含element ='element = ANY(array)' - 數組不包含element ='element <> ALL(array)' – pozs