在SQL中使用LIKE条件

时间:2014-02-15 08:36:42

标签: sql oracle

是否可以在IN中使用LIKE?以下查询会产生错误。

    Select * 
    from employees
    where salary between 2500 and 10000
    and job_id NOT in (like '%MAN%', like '%CLERK%');

正确的语法是什么?我正在使用Oracle 11g

2 个答案:

答案 0 :(得分:4)

你当时不能同时做“喜欢”和“打入”,但你可以这样做

    Select * 
      from employees
     where salary between 2500 
       and 10000
       and job_id NOT like '%MAN%' 
       and job_id NOT like '%CLERK%';

答案 1 :(得分:3)

不幸的是,没有“in like”运算符。您可以将其分解为@Elfentech建议的一系列LIKE条件,或者您可以使用REGEXP_LIKE函数模拟它:

SELECT *
FROM   employees
WHERE  salary BETWEEN 2500 AND 10000
AND    NOT REGEXP_LIKE (job_id, '.*[MAN|CLERK].*')

或者,您可以加入来自UNION ALL的一堆dual次查询:

SELECT *
FROM  employees
JOIN  (SELECT 'MAN' AS search FROM dual 
       UNION ALL
       SELECT 'CLERK' FROM dual) jobs 
ON    employee.jod_id LIKE CONCAT ('%', jobs.search, '%')
WHERE salary BETWEEN 2500 AND 10000