为 SQL Server 组合“LIKE”和“IN”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1865353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:41:11  来源:igfitidea点击:

Combining "LIKE" and "IN" for SQL Server

sqlsql-like

提问by F.P

Is it possible to combine LIKEand INin a SQL Server-Query?

是否可以在 SQL Server 查询中组合LIKEIN

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...

等等...

回答by Fenton

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

实际上,IN 语句创建了一系列 OR 语句……所以

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

可悲的是,这就是你在 LIKE 语句中必须采取的路线

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

回答by lloydz1

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.

你可以用你的 where 子句等进一步扩展它。我只回答了这个,因为这是我正在寻找的,我必须想办法做到这一点。

回答by Adriaan Stander

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

回答by Mitch Wheat

No, you will have to use ORto combine your LIKEstatements:

不,您必须使用OR组合LIKE语句:

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?

你看过全文搜索吗?

回答by Eric J.

You need multiple LIKE clauses connected by OR.

您需要多个由 OR 连接的 LIKE 子句。

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

回答by Andrew Lygin

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

不,MSSQL 不允许这样的查询。你应该使用col LIKE '...' OR col LIKE '...'等。