English 中文(简体)
1. 搜索一栏中载有JSON的阵列中的物项
原标题:Searching a column containing JSON for items in string array

I have a SQL column that contains array data such as:

["Jimmy","Johnny","Jeffry","Jacob","Justin","Joseph","Josh","Jared"] 
or could be 
["Jimmy","Johnny","Jeffry"].

我需要寻找与栏目中所有名字相对应的姓名。 查询可以有1个或全部名字。

I am currently using OPENJSON to filter on individual names like Jimmy or Johnny but now the requirement is if they choose All I need to bring back the columns that only contain ALL the names, not just 1 name so -

["Jimmy","Johnny","Jeffry","Jacob","Justin","Joseph","Josh","Jared"]
 and not 
["Jimmy","Johnny"]

因此,当他们选择统一概念一中的所有名字时,他们就想把即将到来的斜线作为所有名字(上文所列所有名字)的列表。

 @Names =  Jimmy,Johnny,Jeffry,Jacob,Justin,Joseph,Josh,Jared 

我目前正在研究一个单独项目:

 (@Names IN ( SELECT value FROM OPENJSON(a.Names)) OR @Names IS NULL)

我认为,这一要求可能不属于开放Json的范围,但不能确定什么是试图尝试的,或许可能是独立的独立实体。

Any suggestions or questions would be appreciated.

问题回答

并非只是规定在<代码>@上没有名字的条件。 姓名/代码 <代码>中不存在的阵列 页: 1

/*Sample Data*/
DECLARE @T TABLE
  (
     Names NVARCHAR(MAX)
  );

INSERT @T
VALUES (N ["Jimmy","Johnny","Jeffry","Jacob","Justin","Joseph","Josh","Jared"] ),
      ( N ["Jimmy","Johnny","Kevin"] )


/*Example @Names value to simulate your parameter*/
DECLARE @Names NVARCHAR(MAX) = N ["Jimmy","Johnny","Jeffry"] ;

/*Just parse it once, remove any dupes and use PK so SQL Server can see this*/
DECLARE @NamesToFind TABLE(Name NVARCHAR(100) PRIMARY KEY);

INSERT @NamesToFind (Name)
SELECT DISTINCT value
FROM   OPENJSON(@Names)

/*The actual SELECT*/
SELECT *
FROM   @T T
WHERE  NOT EXISTS (SELECT Name
                   FROM   @NamesToFind
                   EXCEPT
                   SELECT value
                   FROM   OPENJSON(T.Names)) 

If @Names is null this will match anything as looks desired from the OR @Names IS NULL in your current code. And if you want to just match a single name then pass in an array with one element.

This will not be able to do any kind of index seek and will require a full scan of some kind.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签