我在查找查询,该查询从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"}}
请有人可以帮助我进行此查询。
答案 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;