我有以下 SQL“产品”表结构:
int Id
nvarchar(max) Details
详细信息包含 JSON 一个具有以下结构的字符串:
{
"Id": "10001",
"Description": "example description",
"Variants": [{
"Title": "ABC / no",
"Price": "10"
}, {
"Title": "ABC / Yes",
"Price": "20",
}, {
"Title": "ABC / Yes",
"Price": "30",
}]
}
我需要编写一个 SQL 查询来查看表并返回具有特定标题的所有变体。
以下作品
从表中获取详细信息字段包含特定标题的所有行
SELECT * FROM Products
WHERE JSON_VALUE(Details, '$.Description') = 'example description'
从表中获取 Details.Variants[0].Title 等于 '{string}' 的所有行
SELECT * FROM Products
WHERE JSON_VALUE(Details, '$.Variants[0].Title') = 'ABC / no'
从 Details.Variants[0].Title 等于 '{string}' 的表中获取所有Id
SELECT JSON_VALUE(Details, '$.Id')
FROM Products
WHERE JSON_VALUE(Details, '$.Variants[0].Title') = 'ABC / no'
我需要从 Product 表中的所有行中获取所有变体,其中 Variant 标题等于“{string}”
本文档中有一个类似的示例, 但我无法让它适用于我的特殊情况。
还有这个堆栈帖子