PostgreQL从Json字段更新多维数组类型的数据

时间:2018-10-29 05:13:23

标签: php sql json postgresql

我在查找查询,该查询从json类型的列的表中更新数据。

我的桌子如下:

ID |    Details

-------------------------------------------------------------------------------------------

1  | {"_translated": {"en_US": {"is_reviewed": "false"}, "default_locale": "en"}}

2  | {"_translated": {"en_US": {"is_reviewed": "false"}, "default_locale": "en"}}

3  | {"_translated": {"en_CA": {"is_reviewed": "true"}, "default_locale": "en"}}

4  | {"_translated": {"en_CA": {"is_reviewed": "false"}, "default_locale": "en"}}

我想将is_reviewed = false的那些行更新为true。

输出:-

ID |    Details

-------------------------------------------------------------------------------------------

1  | {"_translated": {"en_US": {"is_reviewed": "true"}, "default_locale": "en"}}

2  | {"_translated": {"en_US": {"is_reviewed": "true"}, "default_locale": "en"}}

3  | {"_translated": {"en_CA": {"is_reviewed": "true"}, "default_locale": "en"}}

4  | {"_translated": {"en_CA": {"is_reviewed": "true"}, "default_locale": "en"}}

请有人可以帮助我进行此查询。

1 个答案:

答案 0 :(得分:1)

使用jsonb_set(Postgres 9.5 +)

UPDATE t SET details =
jsonb_set(details::jsonb, '{_translated,en_US,is_reviewed}','"true"') where id = 1;
UPDATE t SET details =
jsonb_set(details::jsonb, '{_translated,en_US,is_reviewed}','"true"') where id = 2;
UPDATE t SET details =
jsonb_set(details::jsonb, '{_translated,en_CA,is_reviewed}','"true"') where id = 4;

Demo