postgresql 选择数组中的元素

cyvaqqii  于 2022-11-23  发布在  PostgreSQL
关注(0)|答案(1)|浏览(196)

我有一个JSON,想从数组部分选择数据:

{
  "processedResult": {
    "orderPayment": {
      "paymentType": "VISA"
    },
    "store": "US",
    "orderDeliveryGroups": [
      {
        "address": {
          "city": "New York",
          "state": "NY"
        },
        "deliveryType": "STH"
      }
    ]
  }
}

我现在拥有的

SELECT DISTINCT myDataJsonb -> 'processedResult' -> 'orderPayment' -> 'paymentType'
FROM myData
WHERE myDataJsonb -> 'processedResult' ->> 'store'  = 'US'

所需输出:
| 付款类型|交货类型|
| - -|- -|
| 签证|运输|
| 美国运通|运输|
| 贝宝|燃油|
Postgres版本:PostgreSQL 11.13版本

daolsyd0

daolsyd01#

试试这个

select t.data->'processedResult'->'orderPayment'->'paymentType' as paymentType, 
jsonb_array_elements( t.data->'processedResult' -> 'orderDeliveryGroups')->>'deliveryType' 
   as deliveryType from table_name t
WHERE t.data -> 'processedResult' ->> 'store'  = 'US'

db<>fiddle

相关问题