我在数据库中有一些这样的数据库行 Employee
表格:
EmpId Name Information
1 Eric {“Experience”: [{“Title”: “Assistant Accountant”, “Company”: “ComA”, “YearsOfExperience”: 3}, {“Title”: “Accountant”, “Company”: “ComB”, “YearsOfExperience”: 2}], “EmployedYear”:2016}
2 John {“Experience”: [{“Title”: “Tech Engineer”, “Company”: “ComX”, “Years”: 5}, {“Title”: “Senior Tech Engineer”, “Company”: “ComY”, “YearsOfExperience”: 2}], “EmployedYear”:2012}
3 Leonard {“Experience”: [{“Title”: “Junior Engineer”, “Company”: “ComJ”, “Years”: 2}, {“Title”: “Tech Engineer”, “Company”: “ComB”, “YearsOfExperience”: “7”}], “EmployedYear”:2017}
我如何选择没有在comb工作过的员工?
你对此有何疑问?到目前为止,由于这个复杂的嵌套json数组,我什么都没有得到。
我正在尝试:
SELECT Name, Id
FROM Employee
OUTER APPLY OPENJSON(Information, '$.Experience') WITH (
Title nvarchar(max) '$.Title',
Company nvarchar(max) '$.Company',
YearsOfExperience int '$.YearsOfExperience'
) AS [Info]
WHERE [Info].Company != 'ComB'
3条答案
按热度按时间35g0bw711#
你可以试着用
EXISTS()
以及OPENJSON()
.表格:
声明:
5lwkijsr2#
一种方法是在
HAVING
条款`:cuxqih213#
你可以用
openjson()
和一个侧面连接。您不需要从嵌套对象中提取所有属性,因为您只对公司名称感兴趣:db小提琴演示: