ASP.NET / sql全文搜索

时间:2013-08-19 19:54:47

标签: asp.net sql-server linq

我正在使用以下查询来查找sql​​ server的全文searh。

    DECLARE @MyTable TABLE (Id int identity(1,1), Searched varchar(200))
DECLARE @Keys    TABLE (Word varchar(200), WordId int identity(1,1))

INSERT INTO @MyTable VALUES ('Mother Father Daughter Son')
INSERT INTO @MyTable VALUES ('Mother Daughter Son')
INSERT INTO @MyTable VALUES ('Mother Son')
INSERT INTO @MyTable VALUES ('Daughter Son')
INSERT INTO @MyTable VALUES ('Mother Father Son')
INSERT INTO @MyTable VALUES ('Son Daughter Father')
INSERT INTO @MyTable VALUES ('Mother bun')
INSERT INTO @MyTable VALUES ('Other Word')


INSERT INTO @Keys VALUES ('Mother')
INSERT INTO @Keys VALUES ('Father')
INSERT INTO @Keys VALUES ('Son')
INSERT INTO @Keys VALUES ('Daughter')

DECLARE @nAllWords int
SELECT @nAllWords = COUNT(*) FROM @Keys

SELECT MyTable.*
FROM @MyTable MyTable
INNER JOIN (SELECT MyTable.Id
                   FROM @MyTable MyTable
            INNER JOIN @Keys KeyWords ON ' ' + MyTable.Searched + ' ' LIKE '% ' + KeyWords.Word  + ' %'
            GROUP BY MyTable.Id
            HAVING COUNT(Distinct(KeyWords.Word)) = @nAllWords) Tbl1 ON MyTable.Id = Tbl1.Id

但是上面的代码只返回低于一的单个记录。

Mother Father Daughter Son

我需要结果像完整的4个单词(母亲父女儿子),接下来任何3个单词(母女儿子),接下来任何2个和下一个1 所以我的结果就像

Mother Father Daughter Son
Mother Daughter Son
Mother Father Son
      :
      :

Mother bun

请为我提供多列的建议,我现在尝试使用一列。一旦我接下来,我会尝试多列。

1 个答案:

答案 0 :(得分:0)

如果您从查询中删除以下代码,它将提供所需的结果!

HAVING COUNT(Distinct(KeyWords.Word)) = @nAllWords