如何筛选 Json 数组中的 Json 对象?
How to Filter Json Object in Json Array?
我有一些数据存储在 sql 列中,看起来像
{
"items": [
{ "ids": [4], "fromCompanyId": 4 },
{ "ids": [6, 1], "fromCompanyId": 1 }
]
}
现在我想创建一个 where 子句来查找所有 "items" 的 "fromCompanyId" 为“4”。
目前我发现的是
WHERE JSON_VALUE(jsonInfo,'$.info.address[0].state') LIKE 'US%'
但他们正在对数组索引进行硬编码。我需要找到所有匹配项。
我也在尝试 openJson,但仍然无法正常工作
where 4 in (select fromCompanyId from openjson(Details, '$.items.fromCompanyId') WITH( [fromCompanyId] int '$.fromCompanyId')))
您需要在多个层面上openjson
。像这样。
declare @json nvarchar(max)=N'{
"items": [
{ "ids": [4], "fromCompanyId": 4 },
{ "ids": [6, 1], "fromCompanyId": 1 }
]
}'
select id,fromCompanyId
from openjson(@json,'$.items') j --path to the main array
cross apply openjson(value,'$.ids') -- path inside element of main array
with(id int '$')
cross apply openjson(value)
with (
fromCompanyId int '$.fromCompanyId'
)
where fromCompanyId=4
类似于 table 字段。
declare @tbl table (id int, detail nvarchar(max))
insert @tbl (id,detail) values
(1,N'{
"items": [
{ "ids": [4], "fromCompanyId": 4 },
{ "ids": [6, 1], "fromCompanyId": 1 }
]
}'),
(2,N'{
"items": [
{ "ids": [5], "fromCompanyId": 4 },
{ "ids": [7,9], "fromCompanyId": 4 },
{ "ids": [6, 1], "fromCompanyId": 1 }
]
}')
select id,jid,fromCompanyId
from @tbl
cross apply openjson(detail,'$.items') -- path to the main array
cross apply openjson(value,'$.ids') -- path inside array element
with(jid int '$')
cross apply openjson(value)
with (
fromCompanyId int '$.fromCompanyId'
)
where fromCompanyId=4
SQL 不是该搜索的正确位置。除了这种搜索的性能有多糟糕之外,查询对于像 CPU 和 IO 这样的数据库资源来说将是非常昂贵的。
随着 table 中数据的增长,查询将呈指数级变慢。
如果实际 JSON 超过 8000char(如果存储为 NVARCHAR 则为 4k),即使 w/o 数据增长很多,那么它将被存储在行之外,因此每次它都必须读取 BLOB。
相反,我建议只从数据库中读取并解析 inn 应用程序端,无论您使用何种语言。那会更便宜。
简而言之:这不是 SQL 任务。您应该首先查看工作流程和流程改进。如果搜索查询是常规用户工作流程,那么其自身的架构设计可能不适合此工作流程。
我有一些数据存储在 sql 列中,看起来像
{
"items": [
{ "ids": [4], "fromCompanyId": 4 },
{ "ids": [6, 1], "fromCompanyId": 1 }
]
}
现在我想创建一个 where 子句来查找所有 "items" 的 "fromCompanyId" 为“4”。
目前我发现的是
WHERE JSON_VALUE(jsonInfo,'$.info.address[0].state') LIKE 'US%'
但他们正在对数组索引进行硬编码。我需要找到所有匹配项。
我也在尝试 openJson,但仍然无法正常工作
where 4 in (select fromCompanyId from openjson(Details, '$.items.fromCompanyId') WITH( [fromCompanyId] int '$.fromCompanyId')))
您需要在多个层面上openjson
。像这样。
declare @json nvarchar(max)=N'{
"items": [
{ "ids": [4], "fromCompanyId": 4 },
{ "ids": [6, 1], "fromCompanyId": 1 }
]
}'
select id,fromCompanyId
from openjson(@json,'$.items') j --path to the main array
cross apply openjson(value,'$.ids') -- path inside element of main array
with(id int '$')
cross apply openjson(value)
with (
fromCompanyId int '$.fromCompanyId'
)
where fromCompanyId=4
类似于 table 字段。
declare @tbl table (id int, detail nvarchar(max))
insert @tbl (id,detail) values
(1,N'{
"items": [
{ "ids": [4], "fromCompanyId": 4 },
{ "ids": [6, 1], "fromCompanyId": 1 }
]
}'),
(2,N'{
"items": [
{ "ids": [5], "fromCompanyId": 4 },
{ "ids": [7,9], "fromCompanyId": 4 },
{ "ids": [6, 1], "fromCompanyId": 1 }
]
}')
select id,jid,fromCompanyId
from @tbl
cross apply openjson(detail,'$.items') -- path to the main array
cross apply openjson(value,'$.ids') -- path inside array element
with(jid int '$')
cross apply openjson(value)
with (
fromCompanyId int '$.fromCompanyId'
)
where fromCompanyId=4
SQL 不是该搜索的正确位置。除了这种搜索的性能有多糟糕之外,查询对于像 CPU 和 IO 这样的数据库资源来说将是非常昂贵的。 随着 table 中数据的增长,查询将呈指数级变慢。 如果实际 JSON 超过 8000char(如果存储为 NVARCHAR 则为 4k),即使 w/o 数据增长很多,那么它将被存储在行之外,因此每次它都必须读取 BLOB。
相反,我建议只从数据库中读取并解析 inn 应用程序端,无论您使用何种语言。那会更便宜。
简而言之:这不是 SQL 任务。您应该首先查看工作流程和流程改进。如果搜索查询是常规用户工作流程,那么其自身的架构设计可能不适合此工作流程。