我有JSONB类型的'subject‘列,它存储JSON对象。示例:{"team": "1234", "user": 5678}
或{"org": 123}
或{"team":1234}。
我应该使用什么查询将{"team":"1234",...}的所有匹配项更改为{"team":1234,...}?
我试过了:
UPDATE the_table SET subject = jsonb_set(subject, '{team}', (subject->>'team')::int)
但我得到了:
ERROR: function jsonb_set(jsonb, unknown, integer) does not exist
LINE 2: SET subject = jsonb_set(subject, 'team', (subject->>'team'):...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
发布于 2019-07-04 20:24:46
只需将subject->>'team'
结果直接转换为jsonb
,而不是int
。不要忘记添加WHERE过滤器,否则您的第二条记录将被删除。
UPDATE the_table
SET subject = jsonb_set(subject, '{team}', (subject->>'team')::jsonb)
WHERE subject->>'team' IS NOT NULL;
https://stackoverflow.com/questions/56887776
复制相似问题