[英]Sql Select similar text
I have a table Team 我有一个桌子队
Id Name ...
1 Chelsea
2 Arsenal
3 Liverpool
Now I need to search if my team table has a name like "Chelsea FC". 现在我需要搜索我的队桌是否有像“切尔西足球俱乐部”这样的名字。 How could I make a select query in this case when search string may have extra words ? 在这种情况下,当搜索字符串可能有额外的单词时,如何进行选择查询?
I could try Lucene.net but it's kind of overkill just for a small usage and it would take time to learn it. 我可以尝试使用Lucene.net,但这只是一个小的用途,它有点过分,需要时间来学习它。
You would need to split the string up and search by each word in the string. 您需要将字符串拆分并按字符串中的每个单词进行搜索。 SQL Server doesn't have a native function to do that, but there are various examples on the web. SQL Server没有本机功能,但Web上有各种示例。
This function will take a string and a delimiter, and it will split the string by the delimiter and return a table of the resulting values. 此函数将采用字符串和分隔符,它将通过分隔符拆分字符串并返回结果值的表。
CREATE FUNCTION dbo.SplitVarchar (@stringToSplit varchar(4000), @delimiter CHAR(1))
RETURNS @Result TABLE(Value VARCHAR(50))AS
BEGIN
--This CTE will return a table of (INT, INT) that signify the startIndex and stopIndex
--of each string between delimiters.
WITH SplitCTE(startIndex, stopIndex) AS
(
SELECT 1, CHARINDEX(@delimiter, @stringToSplit) --The bounds of the first word
UNION ALL
SELECT stopIndex + 1, CHARINDEX(@delimiter, @stringToSplit, stopIndex+1)
FROM SplitCTE --Recursively call SplitCTE, getting each successive value
WHERE stopIndex > 0
)
INSERT INTO @Result
SELECT
SUBSTRING(@stringToSplit, --String with the delimited data
startIndex, --startIndex of a particular word in the string
CASE WHEN stopIndex > 0 THEN stopIndex-startIndex --Length of the word
ELSE 4000 END --Just in case the delimiter was missing from the string
) AS stringValue
FROM SplitCTE
RETURN
END;
Once you turn your delimited string into a table, you can JOIN it with the table you wish to search and compare values that way. 一旦将分隔的字符串转换为表格,就可以将其与要搜索的表格一起加入,并以这种方式比较值。
DECLARE @TeamName VARCHAR(50)= 'Chelsea FC'
SELECT DISTINCT Name
FROM Team
INNER JOIN (SELECT Value FROM dbo.SplitVarchar(@TeamName, ' ')) t
ON CHARINDEX(t.Value, Name) > 0
Results: 结果:
| Name |
|---------|
| Chelsea |
I based my design on Amit Jethva's Convert Comma Separated String to Table : 4 different approaches 我的设计基于Amit Jethva的Convert Comma Separated String to Table:4种不同的方法
You can use like
this way: 您可以使用like
这样:
declare @s varchar(20) = 'Chelsey FC'
select * from Team
where name like '%' + @s + '%' or
@s like '%' + name + '%'
This will filter rows if @s
contains Name
or Name
contains @s
. 如果@s
包含Name
或Name
包含@s
这将过滤行。
Use Full Text Search 使用全文搜索
To know more about Full Text Search 了解有关全文搜索的更多信息
http://www.developer.com/db/article.php/3446891/Understanding-SQL-Server-Full-Text-Indexing.htm http://www.developer.com/db/article.php/3446891/Understanding-SQL-Server-Full-Text-Indexing.htm
For implementing it 为了实现它
https://msdn.microsoft.com/en-IN/library/ms142571.aspx https://msdn.microsoft.com/en-IN/library/ms142571.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.