Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在postgreSQL中更新json字段?_Sql_Json_Postgresql_Sql Update - Fatal编程技术网

如何在postgreSQL中更新json字段?

如何在postgreSQL中更新json字段?,sql,json,postgresql,sql-update,Sql,Json,Postgresql,Sql Update,我有下表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'} 我试过

我有下表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 = info || jsonb_build_object('country', info ->> 'country' || id::text)
或者使用
jsonb_set()


官方文件中有一个关于这一点的解释。您可能会对
json_集
jsonb_集
函数感兴趣。

在官方文档中有一个关于这方面的说明。您可能会对
json_集
jsonb_集
函数感兴趣。

构建新对象的速度比运行
jsonb_集
快吗?@JonathanJacobson:我在回答中添加了
jsonb_集()
解决方案。您可能需要根据实际数据集评估这两个选项的性能。构建新对象是否比运行
jsonb_集
更快?@JonathanJacobson:我在回答中添加了
jsonb_集()
解决方案。您可能希望根据实际数据集评估这两个选项的性能。