码迷,mamicode.com
首页 > Web开发 > 详细

修改jsonb的属性

时间:2016-05-27 20:04:30      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

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;

修改jsonb的属性

标签:

原文地址:http://www.cnblogs.com/flintlovesam/p/5535764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!