我有一个 SQL 2016 表,其中包含一个包含 JSON 数据的列。示例 JSON 文档如下所示:
{
"_id": "5a450f0383cac0d725cd6735",
"firstname": "Nanette",
"lastname": "Mccormick",
"registered": "2016-07-10T01:50:10 +04:00",
"friends": [
{
"id": 0,
"name": "Cote Collins",
"interests": [
"Movies",
"Movies",
"Cars"
]
},
{
"id": 1,
"name": "Ratliff Ellison",
"interests": [
"Birding",
"Birding",
"Chess"
]
},
{
"id": 2,
"name": "William Ratliff",
"interests": [
"Music",
"Chess",
"Software"
]
}
],
"greeting": "Hello, Nanette! You have 4 unread messages.",
"favoriteFruit": "apple"
}
我想提取interests
每个friends
对象的数组包含某个值的所有文档。我尝试了这个但没有得到任何结果:
Select *
From <MyTable>
Where 'Chess' IN (Select value From OPENJSON(JsonValue, '$.friends.interests'))
我应该已经返回了几行。我一定没有正确引用兴趣数组或不理解 SQL Server 如何处理这种类型的 JSON 数组。