如果不包含,我如何添加值到postgres数组



你好,我想更新我的数据,我的数据看起来像这样:

list = {sad,dfgdf,vxcvxcv}

我在postgres中使用了数据类型text[]。例如,我想添加poc到这个数组("{sad, dgfdf, vxcvcv}"),如果它不包含poc,如果poc已经在数组中,我不想更新它。还有别的办法吗?

使用@>运算符检查数组中是否包含'poc',如果不包含则添加array_append。这是一个插图。

with t(x, y, arr) as
(
values
('testa', 'BH06', '{sad,dfgdf,vxcvxcv}'::text[]),
('testb', 'BH07', '{das,fdgfd,abcbabc,poc}'),
('testc', 'BH08', '{}')
)
select x, y, 
case when not arr @> '{poc}' then array_append(arr, 'poc') else arr end arr
from t;

检测项目:

<表类>xyarrtbody><<tr>甲壳BH06{悲伤、dfgdf vxcvxcv, poc}的BH07{das, fdgfd abcbabc, poc}testcBH08{poc}