SQL Select 结果为 FREE TEXT / 返回行出现次数更多

SQL Select result with FREETEXT / returning row with more occurences

我有以下 SQL 服务器的搜索词:

我想买三星手机phone型号应该是Galaxy S9

Table:

ID TEXT
1 samsung s8
2 celular s9
3 samsung galaxy s9
4 galasxy s10

我想要 select 到 return ID 3,因为它出现的次数更多。

有很多方法二 select 项。例如:

SELECT 'I want to buy a ' + A.BrandName + ' cellphone and the model should be ' + A.ModelName 
FROM (
    SELECT SUBSTRING(TEXT, 1, LEN('sumsung')) AS BrandName
        , SUBSTRING(TEXT, LEN(SUBSTRING(TEXT, 1, LEN('sumsung')))+1, LEN(TEXT)) AS ModelName
    FROM TABLE_NAME
    WHERE TEXT LIKE N'%samsung%' AND TEXT LIKE N' %galaxy s9%'
) AS A

您可以在 word 级别执行此操作的一种方法是:

select t.*
from t cross apply
     (select count(*) as cnt
      from string_split(t.text, ' ') s1 cross join
           string_split(@sentence, ' ') s2
           on s1.value = s2.value
     ) ss
order by ss.cnt desc;

备注:

  • 这只会在两个短语中查找完全匹配的单词。
  • 这要求单词在 text 和“句子”中都用空格分隔。
  • 重复的单词可能会通过计数。如果需要,这可以进行管理(比如使用 count(distinct s1.value) as cnt)。