[英]How can I update my json field in postgreSQL?
我有下表 myTable :
id info
1 {'email': 'bob@bob.com', 'country': 'USA'}
2 {'email': 'test@test.com', 'country': 'DE'}
我想像这样更新外地国家:
id info
1 {'email': 'bob@bob.com', 'country': 'country1'}
2 {'email': 'test@test.com', 'country': 'country2'}
我试过这个:
UPDATE myTable set info->country : format('country%s', id);
但它不起作用
请问你能帮帮我吗 ?
非常感谢 !
您可以使用||
:
update mytable set info = info || jsonb_build_object('country', info ->> 'country' || id::text)
或者使用jsonb_set()
:
update mytable set info = jsonb_set(info, '{country}', to_jsonb(info ->> 'country' || id::text))
官方文档中有关于这一点的一整章。 您可能会对json_set
或jsonb_set
函数感兴趣。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.