开发者

Combining "LIKE" and "IN" for SQL Server [duplicate]

开发者 https://www.devze.com 2022-12-13 16:49 出处:网络
This question already has answers here: Is there a combination of "LIKE" and "IN" in SQL?
This question already has answers here: Is there a combination of "LIKE" and "IN" in SQL? (28 answers) Closed 9 years ago.

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

So, that this query

SELECT * FROM table WHER开发者_高级运维E column LIKE IN ('Text%', 'Link%', 'Hello%', '%World%')

Finds any of these possible matches:

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

etc...


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%'


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.


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


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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消