Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
Arrays 更新列表Postgres jsonb中的值_Arrays_Postgresql_Sql Update_Jsonb - Fatal编程技术网

Arrays 更新列表Postgres jsonb中的值

Arrays 更新列表Postgres jsonb中的值,arrays,postgresql,sql-update,jsonb,Arrays,Postgresql,Sql Update,Jsonb,我正在尝试更新json [{"id": "1", "name": "myconf", "icons": "small", "theme": "light", "textsize": "large"}, {"id": 2, "name": "myconf2", "theme": "dark"}, {"name": "firstconf", "theme": "dark", "textsize": "large"}, {"id": 3, "name": "firstconxsf", "theme

我正在尝试更新json

[{"id": "1", "name": "myconf", "icons": "small", "theme": "light", "textsize": "large"},
 {"id": 2, "name": "myconf2", "theme": "dark"}, {"name": "firstconf", "theme": "dark", "textsize": "large"},
 {"id": 3, "name": "firstconxsf", "theme": "dassrk", "textsize": "lassrge"}]
这是包含json列的表:

CREATE TABLE USER_CONFIGURATIONS ( ID BIGSERIAL PRIMARY KEY, DATA JSONB ); 
添加新字段很容易,我正在使用:

更新用户配置
SET DATA=DATA | |{“name”:“firstconxsf”,“theme”:“dassrk”,“textsize”:“lassrge”}
其中id=9;
但是如何用id=1或2更新单个

  • 将数组元素展开为每行一个
  • 如果元素具有相关id,则使用
    |
    运算符更新;如果没有,请保留原始版本
  • 更新JSON数据后重新聚合数组
  • 执行
    UPDATE
    语句

  • 创建表用户配置(ID BIGSERIAL主键,数据JSONB);当您尝试使用一个键和列表进行添加时,我很容易理解这一点。我们是否可以在表IDY上输入条件以检查where id=321?您只需添加where子句:它使用相同的id再添加一个值并更新现有值。目前,所有数组行的所有扩展值都将聚合。当然,对于这一点,您需要分组:这有帮助吗?请不要忘记对所有有帮助的答案进行投票(这是对回答者投入到问题中的工作和时间的表彰),并接受最合适的答案(以结束问题)。否则请告诉我们,要改变什么。
    UPDATE users                                                   -- 4
    SET data = s.updated
    FROM (
        SELECT
            jsonb_agg(                                             -- 3
                CASE                                               -- 2
                    WHEN ((elem ->> 'id')::int IN (1,2)) THEN
                        elem || '{"name":"abc", "icon":"HUGE"}'
                    ELSE elem
                END
            ) AS updated
        FROM
            users,
            jsonb_array_elements(data) elem                        -- 1
    ) s;