如何在 JSON 数组中找到等于某个值的记录
How can I find a record that equals a value inside a JSON array
我有以下 Table mytable
id | Json
1 | {"test":[], "partList":[{"partid": 44, "partNum": "1234"}, {"partid": 34, "partNum": "2423"}]}
2 | {"test":[], "partList":[{"partid": 23, "partNum": "8343"}, {"partid": 34, "partNum": "2423"}]}
我正在尝试查询 where id = 2 and partNum = 2423
这是我到目前为止写的:
select *
from mytable
where id = 2
and '2423' IN (select value from OPENJSON(JSON_QUERY(Json, '$.partList'), '$.part'))
最有效的查询方式是什么?
这是对我有用的方法。
select *
from mytable
CROSS apply OPENJSON(Json, '$.partList') WITH ( partnumber nvarchar(100) '$.partNum' )
where partnumber = '2423'
and id = 2
我有以下 Table mytable
id | Json
1 | {"test":[], "partList":[{"partid": 44, "partNum": "1234"}, {"partid": 34, "partNum": "2423"}]}
2 | {"test":[], "partList":[{"partid": 23, "partNum": "8343"}, {"partid": 34, "partNum": "2423"}]}
我正在尝试查询 where id = 2 and partNum = 2423
这是我到目前为止写的:
select *
from mytable
where id = 2
and '2423' IN (select value from OPENJSON(JSON_QUERY(Json, '$.partList'), '$.part'))
最有效的查询方式是什么?
这是对我有用的方法。
select *
from mytable
CROSS apply OPENJSON(Json, '$.partList') WITH ( partnumber nvarchar(100) '$.partNum' )
where partnumber = '2423'
and id = 2