在INNER JOIN的一部分使用LIKE子句[英] Use a LIKE clause in part of an INNER JOIN

本文是小编为大家收集整理的关于在INNER JOIN的一部分使用LIKE子句的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

可以/我应该在构建存储过程/查询时使用类似标准作为内部联接的一部分吗?我不确定我问正确的事情,所以让我解释一下.

我正在创建一个过程,该过程将在包含文本的列中获取要搜索的关键字列表.如果我坐在控制台上,我会这样执行:

SELECT Id, Name, Description
  FROM dbo.Card
 WHERE Description LIKE '%warrior%' 
       OR
       Description LIKE '%fiend%' 
       OR 
       Description LIKE '%damage%'

但是,我在存储过程中解析"强烈键入"列表时,我选择了一个技巧,是将列表解析到表变量/临时表中,将其转换为适当的类型,然后进行内部连接.在我的最终结果集中对抗该表.当将整数ID列表发送到该过程时,这效果很好.我最终有一个看起来像这样的最终查询:

SELECT Id, Name, Description
  FROM dbo.Card
       INNER JOIN @tblExclusiveCard ON dbo.Card.Id = @tblExclusiveCard.CardId

我想将此技巧与字符串列表一起使用.但是,由于我正在寻找一个特定的关键字,因此我将使用类似子句.因此,理想情况下,我认为我的最终查询看起来像这样:

SELECT Id, Name, Description
  FROM dbo.Card
       INNER JOIN @tblKeyword ON dbo.Card.Description LIKE '%' + @tblKeyword.Value + '%'

这是可能的/推荐的吗?

有更好的方法可以做这样的事情吗?


我将通配符放在条款的两端的原因是因为有" Archfiend"," Beast-Warrior"," Direct-Damage"和" Battle-Bake-tamage"术语.

我得到的印象是,根据性能,我可以使用我指定的查询或使用全文关键字搜索来完成相同的任务?

除了让服务器在我要发短信搜索的字段上执行文本索引外,还有其他需要做的事情吗?

推荐答案

您的第一个查询将起作用,但需要进行完整的表扫描,因为该列上的任何索引都将被忽略.您还必须执行一些动态SQL才能生成所有类似的子句.

如果您使用SQL Server,请尝试全文搜索或查看 lucene 实现.乔尔最近谈到了他的成功.

其他推荐答案

尝试此

    select * from Table_1 a
    left join Table_2 b on b.type LIKE '%' + a.type + '%'

这种做法不是理想的.谨慎使用.

其他推荐答案

尝试...

select * from table11 a inner join  table2 b on b.id like (select '%'+a.id+'%') where a.city='abc'.

它对我有效.: - )

本文地址:https://www.itbaoku.cn/post/627785.html

问题描述

Can/Should I use a LIKE criteria as part of an INNER JOIN when building a stored procedure/query? I'm not sure I'm asking the right thing, so let me explain.

I'm creating a procedure that is going to take a list of keywords to be searched for in a column that contains text. If I was sitting at the console, I'd execute it as such:

SELECT Id, Name, Description
  FROM dbo.Card
 WHERE Description LIKE '%warrior%' 
       OR
       Description LIKE '%fiend%' 
       OR 
       Description LIKE '%damage%'

But a trick I picked up a little while go to do "strongly typed" list parsing in a stored procedure is to parse the list into a table variable/temporary table, converting it to the proper type and then doing an INNER JOIN against that table in my final result set. This works great when sending say a list of integer IDs to the procedure. I wind up having a final query that looks like this:

SELECT Id, Name, Description
  FROM dbo.Card
       INNER JOIN @tblExclusiveCard ON dbo.Card.Id = @tblExclusiveCard.CardId

I want to use this trick with a list of strings. But since I'm looking for a particular keyword, I am going to use the LIKE clause. So ideally I'm thinking I'd have my final query look like this:

SELECT Id, Name, Description
  FROM dbo.Card
       INNER JOIN @tblKeyword ON dbo.Card.Description LIKE '%' + @tblKeyword.Value + '%'

Is this possible/recommended?

Is there a better way to do something like this?


The reason I'm putting wildcards on both ends of the clause is because there are "archfiend", "beast-warrior", "direct-damage" and "battle-damage" terms that are used in the card texts.

I'm getting the impression that depending on the performance, I can either use the query I specified or use a full-text keyword search to accomplish the same task?

Other than having the server do a text index on the fields I want to text search, is there anything else I need to do?

推荐答案

Your first query will work but will require a full table scan because any index on that column will be ignored. You will also have to do some dynamic SQL to generate all your LIKE clauses.

Try a full text search if your using SQL Server or check out one of the Lucene implementations. Joel talked about his success with it recently.

其他推荐答案

Try this

    select * from Table_1 a
    left join Table_2 b on b.type LIKE '%' + a.type + '%'

This practice is not ideal. Use with caution.

其他推荐答案

try it...

select * from table11 a inner join  table2 b on b.id like (select '%'+a.id+'%') where a.city='abc'.

Its works for me.:-)