[英]Update column based on array search in postgresql
id | name | status
---+------------------------+--------------------------------
1 | {'Tom','Jerry','Hary'} | { 'Waiting','Waiting',Waiting'}
I want to update status column based on name.我想根据名称更新状态列。
For example, I want to update status column to "Arrived" for name "Hary"例如,我想将名称“Hary”的状态列更新为“到达”
id | name | status
---+------------------------+--------------------------------
1 | {'Tom','Jerry','Hary'} | { 'Waiting',Waiting','Arrived'}
Note: I can achieve by using below query.注意:我可以通过使用以下查询来实现。 But i don't want to use index positioning.
但我不想使用索引定位。 Is there any alternative to achieve above result?
有没有其他方法可以达到上述结果?
UPDATE table
SET status[3] = 'Arrived'
WHERE name[3]='Hary'
As commented by S-Man, you should just fix your data model and store each name/status on a separate record.正如 S-Man 所评论的,您应该只修复您的数据模型并将每个名称/状态存储在单独的记录中。
Otherwise, you can use array_position()
:否则,您可以使用
array_position()
:
update table set status[ array_position(name, 'Harry') ] = 'Arrived'
where 'Harry' = any(name)
GMB has shown you how to do it, but I think you should also see a normalized design (just in case you don't know how that would look): GMB 已经向您展示了如何做,但我认为您还应该看到标准化的设计(以防万一您不知道它会是什么样子):
Name table
-----------
id int
name varchar(70)
Status table
------------
id int
status varchar(100)
namestatus table
----------------
nameid int
statusid int
Data数据
name table
1 Tom
2 Jerry
3 Hary
status table
1 Waiting
2 Arrived
namestatus table
1 1
2 1
3 1
Update statement更新声明
UPDATE namestatus
set statusid = 2
where nameid = 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.