五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

postgres之jsonb屬性的使用操作

發(fā)布日期:2022-01-22 14:34 | 文章來源:腳本之家

jsonb的一些簡單操作(增刪改查)

1、更新操作(attributes屬性為jsonb類型)

方法定義:

jsonb_set(target jsonb, path text[], new_value jsonb[, create_missing boolean])

參數(shù):

target:目標(biāo)(jsonb類型的屬性)

path :路徑,如果jsonb是數(shù)組‘{0,a}'表示在下標(biāo)是0的位置更新a屬性,如果不是數(shù)組,是對象,則寫‘{a}'即可

new_value:新值

選填參數(shù):create_missing:jsonb字段不存在f1屬性時創(chuàng)建,默認(rèn)為true

返回:更新后的jsonb

官方文檔給出的示例(jsonb數(shù)組):

jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false)
結(jié)果:[{"f1":[2,3,4],"f2":null},2,null,3]
jsonb_set('[{"f1":1,"f2":null},2]', '{0,f3}','[2,3,4]')
結(jié)果:[{"f1": 1, "f2": null, "f3": [2, 3, 4]}, 2]

更新jsonb屬性:

-- attributes為jsonb類型字段(對象轉(zhuǎn)成的json)
原值:{"a":"1"}
update user_test set attributes = jsonb_set(attributes,'{a}','"0"'::jsonb, false) where id = '8888';
執(zhí)行后:{"a":"0"}

為jsonb插入屬性:

-- 執(zhí)行后attributes字段中添加了platform:baidu
update user_test set attributes =  attributes::jsonb || '{"platform":"baidu"}'::jsonb;
或者:
update user_test set attributes = jsonb_set(attributes, '{platform}','"baidu"');

查詢

select value from json_each('{"a":"foo", "b":"bar"}') where key = 'a'
select * from json_object_keys('{"a":"foo", "b":"bar"}')
select * from json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}')
select  * from json_object_keys(from ci_type.attributes);--錯誤
select * from to_jsonb('"a":1,"b":2') 
 select '{"a":1,"b":2}'::json->>'b' --獲取jsonb中對應(yīng)鍵的值(文本)
 --select * from json_each( to_jsonb(select distinct attributes from ci_type ) )
 --select to_jsonb(select distinct attributes from ci_type ) 
 
--擴展字段提取相應(yīng)屬性的值
  select  attributes :: json->>'instanceType' from ci_type 
-- 屬性值轉(zhuǎn)為jsonb
select to_jsonb('id:'||id::text) from ci
--jsonb添加屬性,刪除屬性
select '{"a":"foo", "b":"bar"}'::jsonb || '{"c":"fc", "d":"bdd"}'::jsonb--添加
select '{"a":"foo", "b":"bar"}'::jsonb -'c'-'d'-'a'||'{"a":2}'--刪除
select '{"a": "b","c":3}'::jsonb - 'a'
-- 根據(jù)路徑獲取json對象:#>
SELECT '{"a":1,"b":{"ba":"b1","bb":"b2"},"c":3}'::JSON #> '{b,ba}'
結(jié)果:"b1"
SELECT '{"a":1,"b":{"ba":"b1","bb":"b2"},"c":3}'::JSON #> ''
結(jié)果:{"ba":"b1","bb":"b2"}
-- 根據(jù)路徑獲取json對象為text:#>>
SELECT '{"a":1,"b":{"ba":"b1","bb":"b2"},"c":3}'::JSON #>> '{b,ba}'
結(jié)果:"b1"

補充一下吧

1、to_jsonb()方法接受一個參數(shù),將參數(shù)轉(zhuǎn)換為jsonb

jsonb存儲毫秒值字段
# 更新user表中attributes字段中的create_time字段為當(dāng)前時間
update user_test
set attributes = jsonb_set(attributes,'{create_time}',to_jsonb(extract(epoch from now())*1000), true)

2、extract(epoch from now())*1000 獲取毫秒值

EXTRACT(field FROM source)

field 表示取的時間對象,source 表示取的日期來源,類型為 timestamp、time 或 interval。

EXAMPLE:select extract(year from now());

extract(epoch from now())查看現(xiàn)在距1970-01-01 00:00:00 UTC 的秒數(shù)

epoch:新紀(jì)元時間 Epoch 是以 1970-01-01 00:00:00 UTC 為標(biāo)準(zhǔn)的時間,將目標(biāo)時間與 1970-01-01 00:00:00時間的差值以秒來計算 ,單位是秒,可以是負(fù)值;

postgresql操作jsonb數(shù)組

先看表結(jié)構(gòu):

create table person 
(id int, --  唯一標(biāo)識
label jsonb); -- 人的標(biāo)簽數(shù)組(指明某人是哪個公司的),標(biāo)簽時一個一個的對象

label字段數(shù)據(jù)實例

[{"id":1,"code":"p123","name":"ali"},{"id":2,"code":"p123","name":"ali"}]

要求:寫sql實現(xiàn)添加一個標(biāo)簽,刪除一個標(biāo)簽,清空標(biāo)簽;

1、添加一個標(biāo)簽

直接使用 || 符號將兩個jsonb連接成一個jsonb

-- 當(dāng)label為null時
update person set label =  '{"id":1,"code":"p123","name":"ali"}'::jsonb;
-- label不為null時運行
update person set label =  '{"id":1,"code":"p123","name":"ali"}'::jsonb || label

注意:當(dāng)label為null時這樣執(zhí)行最后得到的也是null

2、清空標(biāo)簽

這個比較簡單,我直接設(shè)置為null

update person set label = null;

3、刪除一個標(biāo)簽

這個就比較麻煩一點,我用到了

-> ->> jsonb_array_elements() jsonb_build_array() array()

不熟悉這些符號和函數(shù)的用法的看:http://www.postgres.cn/docs/10/datatype-json.html

update person 
set label = jsonb_build_array(
    array( -- 不使用該函數(shù),當(dāng)篩選出有多于2跳數(shù)據(jù)時會報錯,因為jsonb_build_array函數(shù)只能有一個json
        (select * from 
         (select jsonb_array_elements(label)j from person where id = 1)as a 
         where (j->>'id')::int <> 1) -- 篩選出要刪除的對象
    )
)->0 -- 如果不加這個你會得到兩個[[]]的數(shù)組
where id = 1;

以上就是我解決pg中操作jsonb數(shù)組的方法,希望能給大家一個參考,也希望大家多多支持本站.

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部