繁体   English   中英

如何更改postgresql中数组列中的值

[英]How to change value in array column in postgresql

我有

crew_member_ids                integer[],

表中的列。 并希望将特定 id(例如 1)的所有条目更改为另一个 id(例如 10)

{} - > {}
{1, 2} -> {10, 2}
{2, 3}  -> {2, 3}
{1} -> {10}

我该怎么做 ?


到目前为止,我只设法选择了受影响的行

select * from initial_costs where 1 = any(crew_member_ids);

测试用表

create table test_tabe(crew_member_ids integer[]);
insert into test_tabe values (ARRAY[3,7,4]),(ARRAY[1,8,4]),(ARRAY[1,5]),(ARRAY[8,10,2]);
 crew_member_ids
-----------------
 {3,7,4}
 {1,8,4}
 {1,5}
 {8,10,2}

更改查询

update test_tabe set crew_member_ids[1]=10 where array_position(crew_member_ids,1)=1;
 crew_member_ids
-----------------
 {3,7,4}
 {8,10,2}
 {10,8,4}
 {10,5}
update initial_costs 
set crew_member_ids = array_replace(crew_member_ids, 1, 10) 
where 1 = any(crew_member_ids);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM