我有一列,它的型別是字符變化的。
它包含一個 json 陣列。
像下面的東西。
[
{"name":"Peter", "information":{"tel":"120391083", "address":"xxx"}},
{"name":"Jane", "information":{"tel":"12302131093", "address":"ooo"}},
{"name":"Pat", "information":{"tel":"123098", "address":"zzz"}}
]
如何獲取名稱為 Pat 的 json 物件的地址值?
我使用了 -> 和 ->> 運算子,但它顯示運算子不存在。
我正在使用 pdadmin 4,postgreSQL 版本是 13.2
uj5u.com熱心網友回復:
我不確定您正在尋找什么輸出。
如果你想獲得完整的information部分,你可以使用這個:
select jsonb_path_query_first(the_column::jsonb, '$[*] ? (@.name == "Pat").information')
from the_table;
如果你只有什么address鍵的值,你可以使用:
select jsonb_path_query_first(the_column::jsonb, '$[*] ? (@.name == "Pat").information') ->> 'address'
from the_table;
無論如何jsonb,從長遠來看,您應該將列的資料型別更改為。不要(誤)使用varchar它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/445830.html
標籤:sql json PostgreSQL
