JSON 데이터를 보유하는 열이 포함 된 SQL 테이블이 있습니다.값의 JSON 배열에서 특정 JSON 배열 검색
SELECT
JSON_VALUE(JsonValue, '$.firstname') as FirstName,
JSON_VALUE(JsonValue, '$.lastname') as LastName,
JSON_QUERY(JsonValue, '$.friends') as FriendsList,
From <MyTable>
Where JSON_VALUE(JsonValue,'$.lastname') = 'Green'
쿼리는 서면 같은를 반환
{
"_id": "5a450f038104ca3cb0ff74b5",
"index": 3,
"guid": "20d807c5-bddc-44b9-97fe-fd18af1b6066",
"isActive": false,
"balance": "$2,832.38",
"picture": "http://placehold.it/32x32",
"age": 23,
"eyeColor": "brown",
"firstname": "Genevieve",
"lastname": "Green",
"gender": "female",
"company": "PROFLEX",
"email": "[email protected]",
"phone": "+1 (919) 464-2866",
"address": "107 Clermont Avenue, Rew, California, 4298",
"about": "Magna pariatur ut enim nulla pariatur ad Lorem amet. Proident nulla exercitation id Lorem commodo minim cillum irure exercitation labore nostrud nostrud sint. Voluptate commodo ea commodo quis Lorem laborum culpa voluptate enim nulla enim duis.\r\n",
"registered": "2016-02-16T09:51:25 +05:00",
"latitude": -16.492643,
"longitude": -71.782118,
"tags": [
"in",
"non",
"eiusmod",
"labore",
"dolor",
"laboris",
"ullamco"
],
"friends": [
{
"id": 0,
"name": "Mccoy Berg",
"interests": [
"Music",
"Birding",
"Chess"
]
},
{
"id": 1,
"name": "Chase Mcfadden",
"interests": [
"Software",
"Chess",
"History"
]
},
{
"id": 2,
"name": "Michele Dodson",
"interests": [
"Football",
"Birding",
"Movies"
]
}
],
"greeting": "Hello, Genevieve! You have 2 unread messages.",
"favoriteFruit": "strawberry"
}
나는 다음과 같이 이름과 성 모든 친구를 검색하는 쿼리를 실행할 수 있습니다 : 다음과 같이 JSON 값 중 하나가 보인다
:[
{
"id":0,
"name":"Mccoy Berg",
"interests":[
"Music",
"Birding",
"Chess"
]
},
{
"id":1,
"name":"Chase Mcfadden",
"interests":[
"Software",
"Chess",
"History"
]
},
{
"id":2,
"name":"Michele Dodson",
"interests":[
"Football",
"Birding",
"Movies"
]
}
]
은 내가 실제로 원하는 것은 친구 이름의 단지 배열이 같은 것입니다 : FriendsList에 대한 JSON 문자열은 다음과 같습니다 그
["Mccoy Berg", "Chase Mcfadden", ...]
이것이 가능하지만 JSON 지식은 제한적입니다.
, '$ .friends :
는 먼저, 다음 수
STUFF
그래서 같은FriendsList
필드에 그 이름을, 이름 만 반환하는 하위 쿼리를 작성합니다. 이름 ')'? – DavidG@DavidG - 시도해 보았습니다. NULL을 리턴합니다. –
충분하겠습니까? '[name] NVARCHAR (25) '$ .name')와 함께'SELECT [Name] FROM OPENJSON (@json, '$ .friends')의 이름을 줄 것이다. – DavidG