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

后端 未结 25 1645
灰色年华
灰色年华 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:37

    I may have a solution for this, although it will only work in SQL Server 2008 as far as I know. I discovered that you can use the row-constructor described in https://stackoverflow.com/a/7285095/894974 to join a 'fictional' table using a like clause. It sounds more complex then it is, look:

    SELECT [name]
      ,[userID]
      ,[name]
      ,[town]
      ,[email]
    FROM usr
    join (values ('hotmail'),('gmail'),('live')) as myTable(myColumn) on email like '%'+myTable.myColumn+'%' 
    

    This will result in all users with an e-mail adres like the ones provided in the list. Hope it's of use to anyone. The problem had been bothering me a while.

提交回复
热议问题