简体   繁体   English

从 JSON 数组中提取值以用于 where 子句 - SQL 服务器

[英]Extract value from JSON array for use in where clause - SQL Server

I have a SQL Server table which contains two columns;我有一个包含两列的 SQL 服务器表; Player (VARCHAR), and Availability (JSON/VARCHAR).播放器(VARCHAR) 和可用性(JSON/VARCHAR)。 The data in this table is similar to the below:此表中的数据与以下类似:

Player         Availability
-------+-------------------------------
John   | { "Availability": [1, 3, 6] }
Yusuf  | { "Availability": [3, 4, 7] }
Ben    | { "Availability": [1, 2, 3] }

What I'm wanting is to write a query which will show me what players are available on a certain day (the numbers in the Availability column represent days).我想要的是编写一个查询,它会告诉我某天有哪些玩家可用(可用性列中的数字代表天数)。

Something like:就像是:

SELECT Player 
FROM TableName 
WHERE JSON_VALUE(Availability, '$.Availability') = 3;

This would return: John, Yusuf, and Ben.这将返回:John、Yusuf 和 Ben。

Is this achievable?这是可以实现的吗?

you can use OPENJSON :你可以使用OPENJSON

SELECT t.player, json.value
FROM TableName t
CROSS APPLY OPENJSON(Availability,'$.Availability') json
WHERE json.value = 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM