Shorten SQL Syntax of LIKE Clause to IN Clause

前端 未结 1 743
眼角桃花
眼角桃花 2021-01-16 20:39

In SQL, Is there any way to Shorten syntax From

Select * from TableName
where ColumnName like \'%A%\' or ColumnName like \'%B\' or ColumnName like \'C%\'


        
相关标签:
1条回答
  • 2021-01-16 21:28

    No there isn't a way to combine LIKE with IN directly

    There are ways around this, like this example for SQL Server

    Select *
    from
       TableName T
       JOIN
       (VALUES ('%A%'),('%B'),('C%')) AS X(Expression) ON T.ColumnName LIKE X.Expression
    

    This changes multiple OR search conditions into rows and a JOIN

    Or you can use a UNION to combine several queries in one

    Select * from TableName
    where ColumnName like '%A%'
    union
    Select * from TableName
    where ColumnName like '%B'
    union
    Select * from TableName
    where ColumnName like 'C%'
    
    0 讨论(0)
提交回复
热议问题