【问题标题】:从 Json 数组 MS SQL Server 中选择数据
【发布时间】:2022-01-23 02:06:44
【问题描述】:

我必须像这样从 Json 中选择数据:

[
{
    "id": 10100,
    "externalId": "100000035",
    "name": "Test1",
    "companyId": 10099,
    "phone": "0738003811",
    "email": "test@Test.com",
    "mainAddress": {
      "county": "UK",
      "province": "test",
      "zipCode": "01234",
      "city": "test",
      "street": "test",
      "gln": "44,37489331;26,21941193",
      "country": {
        "iso2": "UK",
        "iso3": "UK"
      }
    },
    "active": false,
    "main": true,
    "stores": [
      "Test"
    ],
    "attributes": [
      {
        "attributeId": 1059,
        "attributeName": "CH6 name",
        "attributeExternalId": null,
        "attributeValueId": 74292,
        "attributeValueType": "MONO_LINGUAL",
        "attributeValueEid": null,
        "attributePlainValue": "Unknown"
      },
      {
        "attributeId": 1061,
        "attributeName": "BD",
        "attributeExternalId": null,
        "attributeValueId": 81720,
        "attributeValueType": "MONO_LINGUAL",
        "attributeValueEid": null,
        "attributePlainValue": "Not assigned"
      }
 
    ],
    "daysSinceLastOrder": null
  },

   {
    "id": 62606,
    "externalId": "VL_LC_000190",
    "name": "Test",
    "companyId": 17793,
    "phone": "44333424",
    "email": "test@email.com",
    "mainAddress": {
      "firmName": "test",
      "county": "test",
      "province": "test",
      "zipCode": "247555",
      "city": "test",
      "street": "test",
      "gln": "44.8773851;23.9223518",
      "country": {
        "iso2": "RO",
        "iso3": "ROU"
      },
      "phone": "07547063789"
    },
    "active": true,
    "main": false,
    "stores": [
      "Valcea"
    ],
    "attributes": [
      {
        "attributeId": 1042,
        "attributeName": "Type of location",
        "attributeExternalId": "TYPE_OF_DIVISION",
        "attributeValueId": 34506,
        "attributeValueType": "MONO_LINGUAL",
        "attributeValueEid": "Small OTC (<40mp)",
        "attributePlainValue": "Small OTC (<40mp)"
      },
      {
        "attributeId": 17,
        "attributeName": "Limit for payment",
        "attributeExternalId": "LIMIT_FOR_PAYMENT_IN_DAYS",
        "attributeValueId": 59120,
        "attributeValueType": "NUMBER",
        "attributeValueEid": null,
        "attributePlainValue": "28"
      } 
    ],
    "daysSinceLastOrder": 147
  }
  ]
  


我知道如何使用“FROM OPENJSON”从简单的 json 对象中选择数据, 但现在我必须选择一个 每个属性的 AttributeValueId、AttributeId 和 AttributeName、attributePlainValue 和 CompanyId。所以我不知道如何从属性数组中选择数据,然后如何加入这个上一层的 CompanyId。 也许有人知道如何编写这个查询。

【问题讨论】:

  • @lptr 你为什么这样做?一旦你完成了小提琴,为什么不直接从中创建一个答案,即使它是一个纯代码答案

标签: sql json sql-server


【解决方案1】:

例如,您可以使用这样的代码。

f1.metaData->"$.identity.customerID" = '.$customerID.'

【讨论】:

    【解决方案2】:

    As mentioned by @lptr in the comments:

    您需要使用CROSS APPLY 将一个OPENJSON 的结果传递给另一个。您可以选择整个 JSON 对象或数组作为属性,使用语法AS JSON

    select
      t1.companyid,
      t2.*
    from openjson(@j)
    with (
      companyId int,
      attributes nvarchar(max) as json
    ) as t1
    cross apply openjson(t1.attributes)
    with
    (
      attributeId int, 
      attributeName nvarchar(100),
      attributeValueId nvarchar(100),
      attributePlainValue nvarchar(100)
    ) as t2;
    

    db<>fiddle

    【讨论】:

      猜你喜欢
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      • 2019-01-19
      相关资源
      最近更新 更多