如何獲取PostgreSQL數(shù)據(jù)庫(kù)中的JSON值
在PostgreSQL數(shù)據(jù)庫(kù)中有一列為JSON,要獲取JSON中得數(shù)據(jù)可以用下面sql:
select orderno as OrderNo ,amount as Amount ,ordertime as OrderTime , recordtype as RecordType from jsonb_to_recordset(( --特定方法 select array_to_json(array_agg(data)) --轉(zhuǎn)換成一個(gè)數(shù)組 from wallet_details where id = @id )::jsonb) as x(orderno text, amount numeric(16, 6), ordertime text, recordtype varchar(32));
如果你獲取得數(shù)據(jù)是當(dāng)前行,但是JSON中也要取出來(lái)幾個(gè)值可以用下面的方式獲?。?/p>
select pay_params::json->>'Key' as Md5Key , pay_params::json->>'AppId' as Appid , pay_params::json->>'MchId' as Mchid , pay_params::json->>'SubMchId' as Submchid , tenant_id as Tenant_Id from spm_wallet_settings where id='12'
補(bǔ)充:PostgreSql數(shù)據(jù)庫(kù)sql語(yǔ)句取Json值
1:json字段實(shí)例:
{ “boxNum”: 0, “orderNum”: 0, “commentNum”: 0 }
A.取boxNum的值
1.1)select 字段名->‘boxNum' from 表名;
1.2)select jsonb_extract_path_text字段名, ‘boxNum') from 表名;
2:json字段實(shí)例:
{ “boxNum”: “0”, “orderNum”: “0”, “commentNum”: “0” }
A.取boxNum的值,不帶雙引號(hào)。
2.1)select 字段名->>‘boxNum' from 表名;
2.2)select jsonb_extract_path_text字段名, ‘boxNum') from 表名;
3:json字段實(shí)例:
{ “unitPrices”: [{ “price”: 10.0, “unitId”: “8”, “unitName”: “500克”, “unitAmount”: “0”, “isPMDefault”: true, “isHomeDefault”: true, “originalPrice”: 10.0 }], “productName”: “遠(yuǎn)洋 加拿大 螯龍蝦 野生捕撈”, “productType”: 1, “skuPortRate”: { “id”: “a6b83048-3878-4698-88c2-2a9de288ac56”, “cityId”: “2bf8c60c-789d-433a-91ae-8e4ae3e587a4”, “dynamicProperties”: [{ “name”: “死亡率”, “propertiesId”: “f05bda8c-f27c-4cc6-b97e-d4bd07272c81”, “propertieValue”: { “value”: “2.0” } }, { “name”: “失水率”, “propertiesId”: “ee9d95d7-7e28-4d54-b572-48ae64146c46”, “propertieValue”: { “value”: “3.0” } }] }, “quotePriceAttribute”: { “currencyName”: “人民幣” } }
A.取quotePriceAttribute中的currencyName幣制名稱
select (字段名>>‘quotePriceAttribute')::json->>‘currencyName' from 表名;
B.取unitPrices中的price單價(jià)
select jsonb_array_elements((字段名->>‘unitPrices')::jsonb)->>‘price' from 表名;
C.取skuPortRate中的dynamicProperties的name為死亡率的propertieValue里面的value;
select bb->‘propertieValue'->>‘value' as value from ( select jsonb_array_elements(((字段名->>‘skuPortRate')::json->>‘dynamicProperties')::jsonb) as bb from 表名) as dd where dd.bb @> ‘{“name”: “死亡率”}';
4.json字段實(shí)例:
[{“name”: “捕撈方式”, “showType”: 4, “propertiesId”: “9a14e435-9688-4e9b-b254-0e8e7cee5a65”, “propertieValue”: {“value”: “野生捕撈”, “enValue”: “Wild”}}, {“name”: “加工方式”, “showType”: 4, “propertiesId”: “7dc101df-d262-4a75-bdca-9ef3155b7507”, “propertieValue”: {“value”: “單凍”, “enValue”: “Individual Quick Freezing”}}, {“name”: “原產(chǎn)地”, “showType”: 4, “propertiesId”: “dc2b506e-6620-4e83-8ca1-a49fa5c5077a”, “propertieValue”: {“value”: “愛(ài)爾蘭”, “remark”: “”, “enValue”: “Ireland”}}]
–獲取原產(chǎn)地
select (SELECT ss->‘propertieValue' as mm FROM (SELECT jsonb_array_elements (dynamic_properties) AS ss FROM product where id=a.id) as dd where dd.ss @> ‘{“name”: “原產(chǎn)地”}')->>‘value' as cuntry, a.* from product as a where a.id=‘633dd80f-7250-465f-8982-7a7f01aaeeec';
5:json例子:huren:[“aaa”,“bbb”,“ccc”…]
需求:取值aaa去““雙引號(hào)”
select replace(cast(jsonb_array_elements(huren) as text), ‘"','') from XXX limit 1
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
PostgreSQL數(shù)據(jù)庫(kù)事務(wù)出現(xiàn)未知狀態(tài)的處理方法
這篇文章主要給大家介紹了PostgreSQL數(shù)據(jù)庫(kù)事務(wù)出現(xiàn)未知狀態(tài)的處理方法,需要的朋友可以參考下2017-07-07postgreSql分組統(tǒng)計(jì)數(shù)據(jù)的實(shí)現(xiàn)代碼
這篇文章給大家介紹postgreSql的監(jiān)控記錄表里多條不同時(shí)間的數(shù)據(jù),只取最新的數(shù)據(jù),并分組統(tǒng)計(jì),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-12-12PostgreSQL數(shù)據(jù)庫(kù)儲(chǔ)存空間不足的解決方案
在使用 PostgreSQL 數(shù)據(jù)庫(kù)時(shí),可能會(huì)遇到存儲(chǔ)空間不足的問(wèn)題,這個(gè)問(wèn)題不僅會(huì)影響數(shù)據(jù)庫(kù)的正常運(yùn)行,還可能導(dǎo)致數(shù)據(jù)丟失或應(yīng)用程序出現(xiàn)故障,因此,了解如何應(yīng)對(duì)這種情況至關(guān)重要,所以本文給大家就介紹了PostgreSQL數(shù)據(jù)庫(kù)儲(chǔ)存空間不足的解決方案,需要的朋友可以參考下2024-07-07postgresql 刪除重復(fù)數(shù)據(jù)的幾種方法小結(jié)
這篇文章主要介紹了postgresql 刪除重復(fù)數(shù)據(jù)的幾種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02使用PostgreSQL的JSONB數(shù)據(jù)類型進(jìn)行高效查詢的示例代碼
PostgreSQL的JSONB數(shù)據(jù)類型提供了一種靈活的方式來(lái)存儲(chǔ)和查詢JSON格式的數(shù)據(jù),下面我們將詳細(xì)討論如何使用JSONB數(shù)據(jù)類型進(jìn)行高效查詢,并提供相應(yīng)的解決方案和示例代碼,需要的朋友可以參考下2024-04-04PostgreSQL數(shù)據(jù)庫(kù)中DISTINCT關(guān)鍵字的四種用法詳解
PostgreSQL 不但高度兼容 SQL 標(biāo)準(zhǔn),同時(shí)還對(duì)很多語(yǔ)法進(jìn)行了擴(kuò)展,可以用于實(shí)現(xiàn)一些特殊的功能,今天我們就來(lái)介紹一下 PostgreSQL 數(shù)據(jù)庫(kù)中 DISTINCT 關(guān)鍵字的 4 種不同用法,需要的朋友可以參考下2024-04-04利用OGG實(shí)現(xiàn)PostgreSQL實(shí)時(shí)同步的過(guò)程詳解
本文詳細(xì)闡述了利用OGG實(shí)現(xiàn)PostgreSQL實(shí)時(shí)同步的全過(guò)程,文章通過(guò)代碼示例和圖文結(jié)合講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11postgresql數(shù)據(jù)庫(kù)導(dǎo)出和導(dǎo)入及常用的數(shù)據(jù)庫(kù)管理工具
數(shù)據(jù)庫(kù)的導(dǎo)入導(dǎo)出是最常用的功能之一,而PostgreSQL提供的對(duì)應(yīng)工具為pg_dump和pg_restore。pg_dump是用于備份PostgreSQL數(shù)據(jù)庫(kù)的工具,下面這篇文章主要給大家介紹了關(guān)于postgresql數(shù)據(jù)庫(kù)導(dǎo)出和導(dǎo)入及常用的數(shù)據(jù)庫(kù)管理工具使用的相關(guān)資料,需要的朋友可以參考下2023-04-04postgresql數(shù)據(jù)合并,多條數(shù)據(jù)合并成1條的操作
這篇文章主要介紹了postgresql數(shù)據(jù)合并,多條數(shù)據(jù)合并成1條的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02