简体   繁体   English

MS SQL 服务器上是否有用于动态列表或多个字符串集的 LIKE 运算符?

[英]Is there a LIKE operator for a dynamic list or set of multiple strings on MS SQL Server?

Suppose I have a result set of only one column containing substrings :假设我的结果集只有一列包含substrings

Table1:表格1:

substrings
-----------
substringa
substringb
substringc
substringd
substringe
etc.
-----------

And I have a second result set from another query:我有另一个查询的第二个结果集:

Table2:表2:

id      | comment
-------------------------------------------------------------------------------
0001    | A text containing substringa
0002    | A text containing substringd
0003    | A text containing none of the substrings from Table1
0004    | Another text containing substringa
0005    | A text containing substringb
...     | etc.
-------------------------------------------------------------------------------

I want to return a third table, Table3, containing the rows in Table2 where the comment contains any of the substrings existing in Table1, including another column with the existing substrings itself, ie:我想返回第三个表 Table3,其中包含 Table2 中的行,其中注释包含 Table1 中存在的任何子substrings ,包括具有现有substrings本身的另一列,即:

Table3:表3:

id      | comment                                | substrings
-------------------------------------------------------------------------------
0001    | A text containing substringa           | substringa
0002    | A text containing substringd           | substringd
0004    | Another text containing substringa     | substringa
0005    | A text containing substringb           | substringb
...     | etc.                                   | ...
-------------------------------------------------------------------------------

It can be assumed that all the comments in Table2 contains exactly zero or one of the substrings in Table1.可以假设 Table2 中的所有注释恰好包含 Table1 中的零个或一个子字符串。 I tried looking for a solution using a combination of charindex , substring , exists or like operators but failed to come up with any solution, and MS SQL Server has no suitable regexp operator that I know of.我尝试使用charindexsubstringexistslike运算符的组合寻找解决方案,但未能提出任何解决方案,并且 MS SQL 服务器没有我知道的合适的正则regexp运算符。 Is there something similar to a like operator to check for multiple strings on MS SQL Server, or are there better methods to do this?是否有类似于like运算符的东西来检查 MS SQL 服务器上的多个字符串,还是有更好的方法来执行此操作? The size of substrings in Table1 is in the order 10^2-10^3 and is dynamically changing, so I cannot hardcode as described in this post . Table1 中子substrings的大小顺序为 10^2-10^3 并且是动态变化的,因此我无法按照本文所述进行硬编码。

One solution would be to use use CHARINDEX() to check if a string contains another string:一种解决方案是使用CHARINDEX()检查一个字符串是否包含另一个字符串:

SELECT t2.*, t1.substring
FROM table2 t2
INNER JOIN table1 t1 ON CHARINDEX(t1.substring, t2.comment) > 0

If you want to see also comments that do not have a matched substring, you can use LEFT JOIN instead of INNER JOIN .如果您还想查看没有匹配的 substring 的注释,您可以使用LEFT JOIN而不是INNER JOIN

LIKE would get the job done as well: LIKE也可以完成工作:

SELECT t2.*, t1.substring
FROM table2 t2
INNER JOIN table1 t1 ON t2.comment LIKE CONCAT('%', t1.substring, '%')

You can try using this query:您可以尝试使用此查询:

SELECT t2.*, t1.substring
FROM table2 t2
INNER JOIN table1 t1 ON t2.comment LIKE '%' + t1.substring + '%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM