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

后端 未结 25 1508
灰色年华
灰色年华 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条回答
  • 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.

    0 讨论(0)
  • 2020-11-22 03:44

    u can even try this

    Function

    CREATE  FUNCTION [dbo].[fn_Split](@text varchar(8000), @delimiter varchar(20))
    RETURNS @Strings TABLE
    (   
      position int IDENTITY PRIMARY KEY,
      value varchar(8000)  
    )
    AS
    BEGIN
    
    DECLARE @index int
    SET @index = -1
    
    WHILE (LEN(@text) > 0)
      BEGIN 
        SET @index = CHARINDEX(@delimiter , @text) 
        IF (@index = 0) AND (LEN(@text) > 0) 
          BEGIN  
            INSERT INTO @Strings VALUES (@text)
              BREAK 
          END 
        IF (@index > 1) 
          BEGIN  
            INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))  
            SET @text = RIGHT(@text, (LEN(@text) - @index)) 
          END 
        ELSE
          SET @text = RIGHT(@text, (LEN(@text) - @index))
        END
      RETURN
    END
    

    Query

    select * from my_table inner join (select value from fn_split('ABC,MOP',','))
    as split_table on my_table.column_name like '%'+split_table.value+'%';
    
    0 讨论(0)
  • 2020-11-22 03:46

    There is no combination of LIKE & IN in SQL, much less in TSQL (SQL Server) or PLSQL (Oracle). Part of the reason for that is because Full Text Search (FTS) is the recommended alternative.

    Both Oracle and SQL Server FTS implementations support the CONTAINS keyword, but the syntax is still slightly different:

    Oracle:

    WHERE CONTAINS(t.something, 'bla OR foo OR batz', 1) > 0
    

    SQL Server:

    WHERE CONTAINS(t.something, '"bla*" OR "foo*" OR "batz*"')
    

    The column you are querying must be full-text indexed.

    Reference:

    • Building Full-Text Search Applications with Oracle Text
    • Understanding SQL Server Full-Text
    0 讨论(0)
  • 2020-11-22 03:48

    I would suggest using a TableValue user function if you'd like to encapsulate the Inner Join or temp table techniques shown above. This would allow it to read a bit more clearly.

    After using the split function defined at: http://www.logiclabz.com/sql-server/split-function-in-sql-server-to-break-comma-separated-strings-into-table.aspx

    we can write the following based on a table I created called "Fish" (int id, varchar(50) Name)

    SELECT Fish.* from Fish 
        JOIN dbo.Split('%ass,%e%',',') as Splits 
        on Name like Splits.items  //items is the name of the output column from the split function.
    

    Outputs

    1   Bass
    2   Pike
    7   Angler
    8   Walleye
    
    0 讨论(0)
  • 2020-11-22 03:48

    In Teradata you can use LIKE ANY ('%ABC%','%PQR%','%XYZ%'). Below is an example which has produced the same results for me

    --===========
    --  CHECK ONE
    --===========
    SELECT *
    FROM Random_Table A
    WHERE (Lower(A.TRAN_1_DSC) LIKE ('%american%express%centurion%bank%')
    OR Lower(A.TRAN_1_DSC) LIKE ('%bofi%federal%bank%')
    OR Lower(A.TRAN_1_DSC) LIKE ('%american%express%bank%fsb%'))
    
    ;
    --===========
    --  CHECK TWO
    --===========
    SELECT *
    FROM Random_Table  A
    WHERE Lower(A.TRAN_1_DSC) LIKE ANY 
    ('%american%express%centurion%bank%',
    '%bofi%federal%bank%',
    '%american%express%bank%fsb%')
    
    0 讨论(0)
  • 2020-11-22 03:49

    you're stuck with the

    WHERE something LIKE 'bla%'
    OR something LIKE '%foo%'
    OR something LIKE 'batz%'
    

    unless you populate a temp table (include the wild cards in with the data) and join like this:

    FROM YourTable                y
        INNER JOIN YourTempTable  t On y.something LIKE t.something
    

    try it out (using SQL Server syntax):

    declare @x table (x varchar(10))
    declare @y table (y varchar(10))
    
    insert @x values ('abcdefg')
    insert @x values ('abc')
    insert @x values ('mnop')
    
    insert @y values ('%abc%')
    insert @y values ('%b%')
    
    select distinct *
    FROM @x x
    WHERE x.x LIKE '%abc%' 
       or x.x LIKE '%b%'
    
    
    select distinct x.*  
    FROM @x             x
        INNER JOIN  @y  y On x.x LIKE y.y
    

    OUTPUT:

    x
    ----------
    abcdefg
    abc
    
    (2 row(s) affected)
    
    x
    ----------
    abc
    abcdefg
    
    (2 row(s) affected)
    
    0 讨论(0)
提交回复
热议问题