簡體   English   中英

PostgreSQL在JSON數組中的數組中搜索

[英]Postgresql search in array in json array

我有列的表:

idworddecode

其中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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM