SQL Query with NOT LIKE IN

前端 未结 8 681
长发绾君心
长发绾君心 2020-12-05 17:15

Please help me to write a sql query with the conditions as \'NOT LIKE IN\'

Select * from Table1 where EmpPU NOT Like IN (\'%CSE%\', \'%ECE%\', \'%EEE%\')


        
相关标签:
8条回答
  • 2020-12-05 17:23

    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';
    
    0 讨论(0)
  • 2020-12-05 17:27

    you can try this

    Select * from Table1 where 
        EmpPU NOT Like '%CSE%'  
    AND EmpPU NOT Like '%ECE%' 
    AND EmpPU NOT Like '%EEE%'
    
    0 讨论(0)
  • 2020-12-05 17:30

    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%'
    
    0 讨论(0)
  • 2020-12-05 17:32

    Or use EXCEPT:

        SELECT * FROM Table1 
        EXCEPT
        SELECT * FROM Table1 
        WHERE EmpPU LIKE '%CSE%'  
           OR EmpPU LIKE '%ECE%' 
           OR EmpPU LIKE '%EEE%'
    
    0 讨论(0)
  • 2020-12-05 17:35

    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%'
    
    0 讨论(0)
  • 2020-12-05 17:35

    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)
    
    0 讨论(0)
提交回复
热议问题