使用 NOT LIKE IN 的 SQL 查询

发布于 2025-01-08 03:41:56 字数 172 浏览 0 评论 0 原文

请帮助我编写一个 SQL 查询,其条件为 NOT LIKE IN

SELECT *
FROM Table1
WHERE EmpPU NOT Like IN ('%CSE%', '%ECE%', '%EEE%');

上面的例子返回一个错误。

Please help me to write an SQL query with the condition as NOT LIKE IN.

SELECT *
FROM Table1
WHERE EmpPU NOT Like IN ('%CSE%', '%ECE%', '%EEE%');

The above example returns an error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

秉烛思 2025-01-15 03:41:56

您不能组合 LIKEIN

下面的语句可以完成这项工作:

SELECT *
FROM Table1 
WHERE EmpPU NOT LIKE '%CSE%' 
  AND EmpPU NOT LIKE '%ECE%' 
  AND EmpPU NOT LIKE '%EEE%';

You cannot combine LIKE and IN.

The statement below would do the job though:

SELECT *
FROM Table1 
WHERE EmpPU NOT LIKE '%CSE%' 
  AND EmpPU NOT LIKE '%ECE%' 
  AND EmpPU NOT LIKE '%EEE%';
丶视觉 2025-01-15 03:41:56

那是因为您将两种语法混合在一起。

如果您始终具有这三个值,则只需对三个 LIKE 表达式的结果进行 AND 运算即可。

SELECT
  *
FROM
  Table1
WHERE
      EmpPU NOT LIKE '%CSE%'
  AND EmpPU NOT LIKE '%ECE%'
  AND EmpPU NOT LIKE '%EEE%'

如果您需要对“任意数量”的值执行此操作,则可以将这些值放入表中并进行联接。

WITH
  myData
AS
(
            SELECT '%CSE%' AS match
  UNION ALL SELECT '%ECE%' AS match
  UNION ALL SELECT '%EEE%' AS match
)

SELECT
  *
FROM
  Table1
LEFT JOIN
  myData
    ON Table1.EmpPU LIKE myData.match
WHERE
  myData.match IS NULL

或者...

WITH
  myData
AS
(
            SELECT '%CSE%' AS match
  UNION ALL SELECT '%ECE%' AS match
  UNION ALL SELECT '%EEE%' AS match
)

SELECT
  *
FROM
  Table1
WHERE
  NOT EXISTS (SELECT * FROM myData WHERE Table1.EmpPU LIKE match)

That's because you're mixing two syntax together.

If you always have exactly those three values, you can just AND the results of three LIKE expressions.

SELECT
  *
FROM
  Table1
WHERE
      EmpPU NOT LIKE '%CSE%'
  AND EmpPU NOT LIKE '%ECE%'
  AND EmpPU NOT LIKE '%EEE%'

If you need to do it for "any number" of values, you can put the values into a table and do a join.

WITH
  myData
AS
(
            SELECT '%CSE%' AS match
  UNION ALL SELECT '%ECE%' AS match
  UNION ALL SELECT '%EEE%' AS match
)

SELECT
  *
FROM
  Table1
LEFT JOIN
  myData
    ON Table1.EmpPU LIKE myData.match
WHERE
  myData.match IS NULL

OR...

WITH
  myData
AS
(
            SELECT '%CSE%' AS match
  UNION ALL SELECT '%ECE%' AS match
  UNION ALL SELECT '%EEE%' AS match
)

SELECT
  *
FROM
  Table1
WHERE
  NOT EXISTS (SELECT * FROM myData WHERE Table1.EmpPU LIKE match)
海未深 2025-01-15 03:41:56

如果您想要在特定列的搜索中包含/排除一组单词。您可能想使用mysql的正则表达式功能。

从列中排除单词集:

SELECT
  *
FROM
  Table1
WHERE
      EmpPU NOT REGEXP 'CSE|ECE|EEE';

从列中搜索单词集:

SELECT
  *
FROM
  Table1
WHERE
      EmpPU REGEXP 'CSE|ECE|EEE';

If you have set of words which you want to include/exclude in search from a particular column. You may want to use regular expression function of mysql.

Exclude set of words from a column :

SELECT
  *
FROM
  Table1
WHERE
      EmpPU NOT REGEXP 'CSE|ECE|EEE';

Search set of words from a column :

SELECT
  *
FROM
  Table1
WHERE
      EmpPU REGEXP 'CSE|ECE|EEE';
变身佩奇 2025-01-15 03:41:56

你不能将 LIKE 和 IN 结合起来,

你可以这样做:

select * from Table1
where EmpPU not in ('%CSE%', '%ECE%', '%EEE%')

但是如果你需要 % ,你不会从 % 通配符中受益,

唯一的选择是:

Select * from Table1
where EmpPU not like '%CSE%' and  EmpPU not like '%ECE%' and EmpPU not like '%EEE%'

you cant combine LIKE and IN

you can do:

select * from Table1
where EmpPU not in ('%CSE%', '%ECE%', '%EEE%')

but you wont benefit from the % wildcard

if you need the % the only option is:

Select * from Table1
where EmpPU not like '%CSE%' and  EmpPU not like '%ECE%' and EmpPU not like '%EEE%'
花想c 2025-01-15 03:41:56

或者你可以这样做:

SELECT 
    * 
FROM 
    Table1
WHERE NOT EXISTS
    (
        SELECT
            NULL
        FROM
        (
            SELECT '%CSE%' AS column1 UNION ALL 
            SELECT '%ECE%' UNION ALL 
            SELECT '%EEE%'
        ) AS tbl
        WHERE Table1.EmpPU LIKE tbl.column1
    )

Or you can do it like this:

SELECT 
    * 
FROM 
    Table1
WHERE NOT EXISTS
    (
        SELECT
            NULL
        FROM
        (
            SELECT '%CSE%' AS column1 UNION ALL 
            SELECT '%ECE%' UNION ALL 
            SELECT '%EEE%'
        ) AS tbl
        WHERE Table1.EmpPU LIKE tbl.column1
    )
暗恋未遂 2025-01-15 03:41:56

你可以试试这个

Select * from Table1 where 
    EmpPU NOT Like '%CSE%'  
AND EmpPU NOT Like '%ECE%' 
AND EmpPU NOT Like '%EEE%'

you can try this

Select * from Table1 where 
    EmpPU NOT Like '%CSE%'  
AND EmpPU NOT Like '%ECE%' 
AND EmpPU NOT Like '%EEE%'
若水微香 2025-01-15 03:41:56

代码如下:

Select * from Table1 where 
        (EmpPU NOT Like '%CSE%'  
    OR EmpPU NOT Like '%ECE%' 
    OR EmpPU NOT Like '%EEE%')

Code is as below:

Select * from Table1 where 
        (EmpPU NOT Like '%CSE%'  
    OR EmpPU NOT Like '%ECE%' 
    OR EmpPU NOT Like '%EEE%')
生来就爱笑 2025-01-15 03:41:56

或者使用EXCEPT

    SELECT * FROM Table1 
    EXCEPT
    SELECT * FROM Table1 
    WHERE EmpPU LIKE '%CSE%'  
       OR EmpPU LIKE '%ECE%' 
       OR EmpPU LIKE '%EEE%'

Or use EXCEPT:

    SELECT * FROM Table1 
    EXCEPT
    SELECT * FROM Table1 
    WHERE EmpPU LIKE '%CSE%'  
       OR EmpPU LIKE '%ECE%' 
       OR EmpPU LIKE '%EEE%'
和我恋爱吧 2025-01-15 03:41:56

您可以使用 SQL Server 的 STRING_SPLIT 并混合使用通配符。

DECLARE @IgnoreWords NVARCHAR(MAX)= '%Word1%,%Word2,Word3%,Word4';

SELECT MyTable.MyCol
FROM
(SELECT 'xxWord1xx' AS MyCol) AS MyTable
WHERE NOT EXISTS
    (SELECT * FROM
        (SELECT value AS ToIgnore FROM STRING_SPLIT(@IgnoreWords, ',')) AS Words
    WHERE MyTable.MyCol LIKE Words.ToIgnore)

You can use SQL Server's STRING_SPLIT and use a mix of wildcards.

DECLARE @IgnoreWords NVARCHAR(MAX)= '%Word1%,%Word2,Word3%,Word4';

SELECT MyTable.MyCol
FROM
(SELECT 'xxWord1xx' AS MyCol) AS MyTable
WHERE NOT EXISTS
    (SELECT * FROM
        (SELECT value AS ToIgnore FROM STRING_SPLIT(@IgnoreWords, ',')) AS Words
    WHERE MyTable.MyCol LIKE Words.ToIgnore)

八巷 2025-01-15 03:41:56

这与其他示例类似,但使用 CTE 和 SQL Server 表值构造函数。参考文献:

这个是类似的使用 UNIONSTRING_SPLIT 示例,但当我更改匹配条件列表并且列表变得相当长时,我发现此语法稍微干净一些。您还可以将不同的v CTE 连接到其他一些表,以实现更复杂的场景。

;WITH unlike AS 
(
    SELECT v.notme
    FROM (VALUES
        ('%CSE%'),
        ('%ECE%'),
        ('%EEE%')
        ) AS v (notme) 
)
SELECT * 
FROM Table1 AS t
WHERE NOT EXISTS (SELECT * FROM unlike WHERE t.EmpPU LIKE unlike.notme)

This is similar to others examples but uses a CTE and a SQL Server table value constructor. References:

This is similar to using UNION or the STRING_SPLIT examples but I find this syntax slightly cleaner when I am changing the match conditions list and the list gets quite long. You could also join your unlike v CTE to some other table for a more complex scenario.

;WITH unlike AS 
(
    SELECT v.notme
    FROM (VALUES
        ('%CSE%'),
        ('%ECE%'),
        ('%EEE%')
        ) AS v (notme) 
)
SELECT * 
FROM Table1 AS t
WHERE NOT EXISTS (SELECT * FROM unlike WHERE t.EmpPU LIKE unlike.notme)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文