zoukankan      html  css  js  c++  java
  • 修改jsonb的属性

    CREATE FUNCTION jsonb_merge(JSONB, JSONB)

    RETURNS JSONB AS $$

    WITH json_union AS (

        SELECT * FROM JSONB_EACH($1)

        UNION ALL

        SELECT * FROM JSONB_EACH($2)

    ) SELECT JSON_OBJECT_AGG(key, value)::JSONB FROM json_union;

    $$ LANGUAGE SQL;

     

    UPDATE xxx SET data = jsonb_merge(data,'{"b":3}') WHERE id = 1;

    SELECT * FROM xxx;

     id |       data      

    ----+------------------

      1 | {"a": 1, "b": 3}

     

    -- append

    UPDATE xxx SET data = jsonb_merge(data,'{"c":4}') WHERE id = 1;

    SELECT * FROM xxx;

     id |           data      

    ----+-------------------------

      1 | {"a": 1, "b": 3, "c": 4}

     

     

     

     

    CREATE FUNCTION jsonb_merge(JSONB, JSONB)

    RETURNS JSONB AS $$

    WITH json_union AS (

        SELECT * FROM JSONB_EACH($1)

        UNION ALL

        SELECT * FROM JSONB_EACH($2)

    ) SELECT JSON_OBJECT_AGG(key, value)::JSONB

         FROM json_union

         WHERE key NOT IN (SELECT key FROM json_union WHERE value ='null');

    $$ LANGUAGE SQL;

  • 相关阅读:
    正则表达式入门教程
    js获取class
    锋利的jQuery第6章 jQuery与Ajax的应用
    显示隐藏左侧菜单
    unicode转为汉字
    $.ajax
    .ashx文件
    c#正则表达式
    调试发现的小错误
    sql2005连接不到本地数据库
  • 原文地址:https://www.cnblogs.com/flintlovesam/p/5535764.html
Copyright © 2011-2022 走看看