簡體   English   中英

數組中的psql jsonb數組

[英]psql jsonb array within a array

我的數據結構就像數組中的數組。 我有一個名為 id (int)、meta (JSONB) 的 2 列表,其中數據存儲如下:

id : meta
12 : [... ]

[...] 如下所示:

[
  {
    "task": "T3",
    "task_label": "what is wrong.",
    "value": "Something's wrong"
  },
  {
    "task": "T0",
    "task_label": "What's wrong about this image?",
    "value": [
      {
        "x": 228.52696228027344,
        "y": 42.95765686035156,
        "tool": 0,
        "frame": 0,
        "width": 738.8717193603516,
        "height": 45.10553741455078,
        "details": [],
        "tool_label": "Sender"
      },
      {
        "x": 1302.4683837890625,
        "y": 642.2169799804688,
        "tool": 2,
        "frame": 0,
        "width": 423.1329345703125,
        "height": 115.98565673828125,
        "details": [],
        "tool_label": "Action"
      }
    ]
  }
]

我想運行 sql 查詢來查找任務 =“T0”的所有工具標簽的列表。

結果應該是這樣的

id: meta->task->value->tool_label
12: Sender, Action 

您可以使用jsonb_array_elements()兩次,然后過濾和聚合:

select t.id, string_agg(l2.obj ->> 'tool_label', ',') tool_labels
from mytable t
cross join lateral jsonb_array_elements(t.meta)            as l1(obj)
cross join lateral jsonb_array_elements(l1.obj -> 'value') as l2(obj)
where l1.obj ->> 'task' = 'T0'
group by t.id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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