Combining “LIKE” and “IN” for SQL Server [duplicate]

扶醉桌前 提交于 2019-11-26 01:12:44

问题


Is it possible to combine LIKE and IN in a SQL Server-Query?

So, that this query

SELECT * FROM table WHERE column LIKE IN (\'Text%\', \'Link%\', \'Hello%\', \'%World%\')

Finds any of these possible matches:

Text, Textasd, Text hello, Link2, Linkomg, HelloWorld, ThatWorldBusiness

etc...


回答1:


Effectively, the IN statement creates a series of OR statements... so

SELECT * FROM table WHERE column IN (1, 2, 3)

Is effectively

SELECT * FROM table WHERE column = 1 OR column = 2 OR column = 3

And sadly, that is the route you'll have to take with your LIKE statements

SELECT * FROM table
WHERE column LIKE 'Text%' OR column LIKE 'Hello%' OR column LIKE 'That%'



回答2:


I know this is old but I got a kind of working solution

SELECT Tbla.* FROM Tbla
INNER JOIN Tblb ON
Tblb.col1 Like '%'+Tbla.Col2+'%'

You can expand it further with your where clause etc. I only answered this because this is what I was looking for and I had to figure out a way of doing it.




回答3:


One other option would be to use something like this

SELECT  * 
FROM    table t INNER JOIN
        (
            SELECT  'Text%' Col
            UNION SELECT 'Link%'
            UNION SELECT 'Hello%'
            UNION SELECT '%World%'
        ) List ON t.COLUMN LIKE List.Col



回答4:


No, you will have to use OR to combine your LIKE statements:

SELECT 
   * 
FROM 
   table
WHERE 
   column LIKE 'Text%' OR 
   column LIKE 'Link%' OR 
   column LIKE 'Hello%' OR
   column LIKE '%World%'

Have you looked at Full-Text Search?




回答5:


You need multiple LIKE clauses connected by OR.

SELECT * FROM table WHERE 
column LIKE 'Text%' OR 
column LIKE 'Link%' OR 
column LIKE 'Hello%' OR 
column LIKE '%World%' OR 



回答6:


No, MSSQL doesn't allow such queries. You should use col LIKE '...' OR col LIKE '...' etc.



来源:https://stackoverflow.com/questions/1865353/combining-like-and-in-for-sql-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!