没有LIKE IN的SQL查询

时间:2012-02-22 10:55:34

标签: sql sql-server-2008 tsql

请帮我写一个条件为'NOT LIKE IN'

的SQL查询
Select * from Table1 where EmpPU NOT Like IN ('%CSE%', '%ECE%', '%EEE%')

得到错误。

8 个答案:

答案 0 :(得分:78)

你不能将喜欢和结合起来。下面的声明可以完成这项工作:

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

答案 1 :(得分:16)

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

如果你总是拥有这三个值,你可以只选择三个LIKE表达式的结果。

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

... 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)

答案 2 :(得分:8)

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

从列中排除单词集:

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

搜索列中的单词集:

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

答案 3 :(得分:5)

你不能把LIKE和IN结合起来

你可以这样做:

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

但你不会受益于%wildcard

如果你需要%,唯一的选择是:

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

答案 4 :(得分:1)

或者你可以这样做:

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
    )

答案 5 :(得分:0)

你可以试试这个

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

答案 6 :(得分:0)

代码如下:

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

答案 7 :(得分:0)

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

declare @sexo as char(1)

select @sexo='F'

select * from pessoa

where isnull(Sexo,0) =isnull(@Sexo,0)