我正在使用以下查询进行 sql server 的全文搜索。
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
请就多列向我提出建议,我现在正在尝试一列。一旦我得到这个,我将尝试多列。