我正在尝试在JSON数据上使用OpenJSON,但未按预期产生结果。 field_data中的DB JSON为:
SELECT *
FROM beat_data
WHERE EXISTS (
Select *
FROM OPENJSON(field_data,'$.name')
WHERE Value = 'Test'
)
我正在尝试获取名称等于field_data列中的Test的数据。 我的查询数据是;
Date d1 = bean.fooDate; // >> from a post request
System.out.println(d1); // >> "Sat May 25 10:00:00 WET 2019"
Date d2 = fooEntity.getFooDate(); // >> from mysql database
System.out.println(d2); // >> "2019-05-25"
我觉得我没有正确使用路径。
答案 0 :(得分:2)
您可以尝试使用JSON_VALUE()
:
输入:
CREATE TABLE #BeatData (
JsonColumn nvarchar(max)
)
INSERT INTO #BeatData
(JsonColumn)
VALUES
(N'{
"name":"Test",
"Cell-Number":"Test",
"CNIC":"12112112",
"Person-Name":"Test",
"Focal-person-cell-phone":"121212",
"Focal-person-CNIC":"12121212",
"Religion":"Test",
"Total-teachers":"4",
"Total-students":"37",
"Total-Hosteled-students":"11",
"Government-\/-Private":"Govt",
"Category":"Jamia",
"Total-Gates":"3",
"Registration-number":"211121",
"Security-guard\/watchman":"10",
"Condition":"Test"
}')
T-SQL:
SELECT *
FROM #BeatData
WHERE JSON_VALUE(JsonColumn, '$.name') = N'Test'
如果要获取JSON
数据作为结果集,请尝试以下操作:
SELECT j.*
FROM #BeatData b
CROSS APPLY OPENJSON(b.JsonColumn) WITH (
name varchar(100) '$.name',
[Cell-Number] varchar(100) '$."Cell-Number"',
CNIC varchar(100) '$.CNIC',
[Person-Name] varchar(100) '$."Person-Name"',
[Focal-person-cell-phone] varchar(100) '$."Focal-person-cell-phone"',
[Focal-person-CNIC] varchar(100) '$."Focal-person-CNIC"',
Religion varchar(100) '$.Religion',
[Total-teachers] varchar(100) '$."Total-teachers"',
[Total-students] varchar(100) '$."Total-students"',
[Total-Hosteled-students] varchar(100) '$."Total-Hosteled-students"',
[Government-\/-Private] varchar(100) '$."Government-\/-Private"',
Category varchar(100) '$.Category',
[Total-Gates] varchar(100) '$."Total-Gates"',
[Registration-number] varchar(100) '$."Registration-number"',
[Security-guard\/watchman] varchar(100) '$."Security-guard\/watchman"',
Condition varchar(100) '$.Condition'
) j
WHERE j.name = N'Test'