Is there a combination of “LIKE” and “IN” in SQL?

后端 未结 25 1661
灰色年华
灰色年华 2020-11-22 03:08

In SQL I (sadly) often have to use \"LIKE\" conditions due to databases that violate nearly every rule of normalization. I can\'t change that right now. But tha

25条回答
  •  -上瘾入骨i
    2020-11-22 03:41

    If you want to make your statement easily readable, then you can use REGEXP_LIKE (available from Oracle version 10 onwards).

    An example table:

    SQL> create table mytable (something)
      2  as
      3  select 'blabla' from dual union all
      4  select 'notbla' from dual union all
      5  select 'ofooof' from dual union all
      6  select 'ofofof' from dual union all
      7  select 'batzzz' from dual
      8  /
    
    Table created.
    

    The original syntax:

    SQL> select something
      2    from mytable
      3   where something like 'bla%'
      4      or something like '%foo%'
      5      or something like 'batz%'
      6  /
    
    SOMETH
    ------
    blabla
    ofooof
    batzzz
    
    3 rows selected.
    

    And a simple looking query with REGEXP_LIKE

    SQL> select something
      2    from mytable
      3   where regexp_like (something,'^bla|foo|^batz')
      4  /
    
    SOMETH
    ------
    blabla
    ofooof
    batzzz
    
    3 rows selected.
    

    BUT ...

    I would not recommend it myself due to the not-so-good performance. I'd stick with the several LIKE predicates. So the examples were just for fun.

提交回复
热议问题