[英]Postgresql search in array in json array
我有列的表:
id
, word
, decode
其中decode
是一個像這樣的json數組:
[{
"character": "三",
"components": [
"一",
"二"
]
},
{
"character": "大",
"components": [
"大"
]
},
{
"character": "洋",
"components": [
"氵",
"羊"
]
}]
我需要組件中的搜索組件。 例如“二”
您可以使用json_array_elements_text(json)
函數將json
數組擴展為一組文本值,然后按所需值進行過濾:
select q.arrays
from
(
select json_array_elements_text(decode) as arrays
from tab
) q
where q.arrays like '%二%';
arrays
---------------------------------------------------
{ "character": "三", "components": [ "一", "二" ]}
如果您確實想提取包含您的字符( "二"
)的子組件,請使用:
select *
from
(
select elms -> 'components' as elm
from tab t,
json_array_elements(decode) as elms
) q
where elm::text like '%二%';
elm
---------------------------------------------------
[ "一", "二" ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.