1

我有 JSON 文档。结构/样本数据就像 -

{
   "id":"201",
   "portfolio":[
      {
         "portfolio_id":"PORTFOLIO_001",
         "portfolio_name":"AAA",
         "product":[
            {
               "product_id":"PORTFOLIO_001_PRODUCT_001",
               "product_trigram":"PORTFOLIO_001_PRODUCT_001_1",
               "product_name":"PORTFOLIO_001_PRODUCT_001_NAME"
            },
            {
               "product_id":"PORTFOLIO_001_PRODUCT_002",
               "product_trigram":"PORTFOLIO_001_PRODUCT_002_1",
               "product_name":"PORTFOLIO_001_PRODUCT_002_NAME"
            }
         ]
      },
      {
         "portfolio_id":"PORTFOLIO_002",
         "portfolio_name":"BBB",
         "product":[
            {
               "product_id":"PORTFOLIO_002_PRODUCT_001",
               "product_trigram":"PORTFOLIO_002_PRODUCT_001_1",
               "product_name":"PORTFOLIO_002_PRODUCT_001_NAME"
            }
         ]
      }
   ]
}

我已经编写了如下的选择 SQL。我想获取该特定产品组合(PORTFOLIO_001)和 id 的产品。我得到所有投资组合的所有产品,而不是特定投资组合的产品(PORTFOLIO_001)

SELECT json_extract(j, '$.portfolio[*].product') FROM t WHERE json_contains(json_extract(j, '$.portfolio[*].portfolio_id'), '"PORTFOLIO_001"') AND JSON_CONTAINS(j, '"201"', '$.id')

实际输出 -

[
   [
      {
         "product_id":"PORTFOLIO_001_PRODUCT_001",
         "product_trigram":"PORTFOLIO_001_PRODUCT_001_1",
         "product_name":"PORTFOLIO_001_PRODUCT_001_NAME"
      },
      {
         "product_id":"PORTFOLIO_001_PRODUCT_002",
         "product_trigram":"PORTFOLIO_001_PRODUCT_002_1",
         "product_name":"PORTFOLIO_001_PRODUCT_002_NAME"
      }
   ],
   [
      {
         "product_id":"PORTFOLIO_002_PRODUCT_001",
         "product_trigram":"PORTFOLIO_002_PRODUCT_001_1",
         "product_name":"PORTFOLIO_002_PRODUCT_001_NAME"
      }
   ]
]

预期产出 -

[
    {
     "product_id":"PORTFOLIO_001_PRODUCT_001",
     "product_trigram":"PORTFOLIO_001_PRODUCT_001_1",
     "product_name":"PORTFOLIO_001_PRODUCT_001_NAME"
    },
    {
     "product_id":"PORTFOLIO_001_PRODUCT_002",
     "product_trigram":"PORTFOLIO_001_PRODUCT_002_1",
     "product_name":"PORTFOLIO_001_PRODUCT_002_NAME"
    }
]

有人可以帮我解决这个问题吗?

提前感谢您的行动:-)

问候, 普拉尚特

4

0 回答 0