开发者

SQL IN Statement using like syntax? [duplicate]

开发者 https://www.devze.com 2022-12-24 23:42 出处:网络
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.

I would like to do something like this i.e., use wild card characters in the in clause:

SELECT * FROM mytable WHERE keywords IN ('%test%', '%testing%')

This is not supported in SQL Server.... Is there some other way to achieve it开发者_运维问答...

Looking for something other than:

SELECT * FROM mytable WHERE keywords like '%test%' or keywords like '%testing%' or.....


Nope, only other way I can think of is joining in to a temp table but then you have to eliminate duplicate rows.

This query of yours is horribly inefficient as it will unconditionally table scan. Perhaps you should look at separating the keywords or introducing a full text index.

If you would like to look into doing full text searches, then I would recommend looking into some of the heavy weights out there like sphinx or lucene when you evaluate the Sql Server full-text solution.


SELECT DISTINCT *
FROM
  mytable M
  JOIN
  (
  SELECT '%test%' AS pattern
  UNION ALL SELECT '%testing%'
  ...
  ) foo ON M.keywords LIKE foo.Pattern

Could be a CTE or temp table too


This is not supported in MS SQL.... Is there some other way to achieve it?

Yes, Full Text Search. At least for prefix wildcards.


You can use Regular Expression to do this. In one Regular Expression you can define as many pattern as you want in one expression.

There are lot's of article about Regular Expression Matching in MS SQL SERVER. I used RLIKE in MySQL to do Regular Expression Matching.

I also attached link1 & link2 for MS SQL SERVER Regular Expression Matching.

0

精彩评论

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