简体   繁体   English

SQL Server:从动态值表中选择列不包含任何值的所有行

[英]SQL Server : select all rows where Column does not contain any value from dynamic table of values

I'm using SQL Server 2016. I am searching TableA and want it to not return any rows where one of the terms from TableB exists in a particular column of TableA. 我正在使用SQL Server2016。我正在搜索TableA,并希望它不返回TableA的特定列中存在TableB之一的术语的任何行。

Assume I have the following sample tables: 假设我有以下示例表:

DECLARE @SearchTerms TABLE (word NVARCHAR(10))

INSERT INTO @SearchTerms
    SELECT
        v
    FROM 
        (VALUES ('ABC'), ('DEF')) vals(v)

SELECT * FROM @SearchTerms

DECLARE @MyStrings TABLE 
                   (
                        ID  INT,
                        string NVARCHAR(MAX)
                   )

INSERT INTO @MyStrings
    SELECT
        v, x
    FROM 
        (VALUES (1, 'This is the first sentence and has nothing'),
                (2, 'This is the second sentence and has ABC only'),
                (3, 'This is the third sentence and has DEF only'),
                (4, 'This is the fourth sentence and has ABC and DEF together')) vals(v,x)

SELECT * FROM @MyStrings

In table @SearchTerms , I have ABC and DEF. 在表@SearchTerms ,我有ABC和DEF。 I want to select * from table @MyStrings where string value does not contain ABC or DEF. 我想select * from table @MyStrings中select * from table其中字符串值不包含ABC或DEF。

Something like this: 像这样:

SELECT * 
FROM @MyStrings
WHERE string NOT LIKE (SELECT word FROM @SearchTerms)

If the search terms aren't nullable, you can left join the search terms using LIKE and filter for all rows, where the search term is null. 如果搜索项不可为空,则可以使用LIKE保留搜索项的连接性,并过滤搜索项为null的所有行。

SELECT s.*
       FROM @mystrings s
            LEFT JOIN @searchterms t
                      ON s.string LIKE concat('%', t.word, '%')
       WHERE t.word IS NULL;

db<>fiddle 分贝<>小提琴

If they are nullable excluding them in the ON clause might work. 如果它们可以为空,则在ON子句中排除它们可能会起作用。

SELECT s.*
       FROM @mystrings s
            LEFT JOIN @searchterms t
                      ON s.string LIKE concat('%', t.word, '%')
                         AND t.word IS NOT NULL
       WHERE t.word IS NULL;

暂无
暂无

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

相关问题 从表中选择匹配的行,其中两列之一包含值列表中的任何值 - Select matching rows from a table where either one of two columns contain any value from a list of values 如何从表中选择组不包含值的行 - How to select rows from table where group does not contain value SQL选择链接表中所有行在x列中具有相同值的行 - SQL Select rows where all rows from linked table have the same value in column x SQL Server:选择行,其中列中的值从值列表更改为另一个值列表 - SQL Server: Select rows where value in column changes from list of values to another list of values SQL Server:如何在列中选择具有相同值的行,但在另一列上为分组行选择一些精确值 - SQL Server : how to select the rows in a table with the same value on a column but some exact values on another column for the grouped rows 从表中选择一列的值不同于该列的先前值的所有行 - Select all rows from a table where value on a column differs from previous value on that column SQL Server:选择所有行,如果4列中的任何一列的值为“ N” - SQL Server: Select all rows where if any one of the 4 columns has a value of 'N' SQL - 选择列中所有值相同的行 - SQL - Select rows where all values in a column are the same SQL从表中选择所有连接的行都匹配值的行 - SQL Select rows from table where all joined rows match value 表的SELECT计数,其中列为CONNECT,但与其他相关列相关的其他行没有特定值 - SELECT count from table where column is CONNECT but other rows with related other column does not have a certain value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM