SQL:喜欢与包含 - 不同的结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7688891/
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
SQL: Like vs Contains - Different Results
提问by Corey
I'm running two queries on a table.
我在一个表上运行两个查询。
SELECT MSDS FROM dbo.MSDSSearch3 WHERE CONTAINS(MSDS, 'STYCAST')
And
和
SELECT MSDS FROM dbo.MSDSSearch3 WHERE MSDS like '%STYCAST%'
The first query will return
第一个查询将返回
'STYCAST 50300 LV'
And the second will return
第二个将返回
'STYCAST 50300 LV'
'STYCAST 2851 BLACK'
Does anyone know why the like would return more values than the contains? Is there a problem with how I'm running contains? Thanks in advance.
有谁知道为什么喜欢会返回比包含更多的值?我的运行方式有问题吗?提前致谢。
采纳答案by DougEC
Here's a similar post, where rebuilding the fulltext catalog seemed to solve the problem:
这是一个类似的帖子,其中重建全文目录似乎解决了问题:
SQL Problem: Using CONTAINS() doesn't work, but LIKE works fine
回答by daniloquio
CONTAINS is a totally different function, it is a predicate based query for full-text columns; it is not a function to determine if a column contain a string in it.
CONTAINS 是一个完全不同的函数,它是一个基于谓词的全文列查询;它不是确定列中是否包含字符串的函数。
For the query you are running, you could use this:
对于您正在运行的查询,您可以使用以下命令:
SELECT MSDS FROM dbo.MSDSSearch3 WHERE CONTAINS(MSDS, '"STYCAST*"')
There you have a prefix search, instead of a simple_term search like you currently have.
在那里你有一个前缀搜索,而不是像你目前那样的 simple_term 搜索。
More details: http://msdn.microsoft.com/en-us/library/ms187787.aspx
更多详情:http: //msdn.microsoft.com/en-us/library/ms187787.aspx
Maybe in the way you are using it the text 'STYCAST 2851 BLACK' don't fall into results because it have one more character than 'STYCAST 50300 LV', so it is a [7 of 17 match] vs a [7 of 16 match]. I'm not sure but that could explain this strange behavior.
也许在您使用它的方式中,文本“STYCAST 2851 BLACK”不会归入结果,因为它比“STYCAST 50300 LV”多一个字符,因此它是 [7 of 17 match] 与 [7 of 16]比赛]。我不确定,但这可以解释这种奇怪的行为。