查找序列词SQL

时间:2015-02-25 07:36:47

标签: sql sql-server search

我正在使用SQL Server 2008,我需要在Words表中搜索完整的句子。

Words

ID          LineNum     WordText
----------- ----------- -----------
1           1           i
2           1           love
3           2           i
4           2           love
5           2           ice
6           3           i
7           3           love
8           3           dogs
9           3           too

如果句子是“我喜欢狗”,那么这种情况下的结果应该只是ID 6-8。

ID          LineNum     WordText
----------- ----------- ------------
6           3           i
7           3           love
8           3           dogs

3 个答案:

答案 0 :(得分:3)

试试这个:

SELECT T1.linenum, 
       word = STUFF((
          SELECT ' ' + T2.wordtext
          FROM TableName T2
          WHERE T1.linenum = T2.linenum
          FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM TableName T1
GROUP BY T1.linenum
ORDER BY T1.linenum

结果:

LINENUM     WORD
----------------------------
1           i love
2           i love ice
3           I love dogs too

请参阅SQL Fiddle中的结果。

修改

作为列表的结果,这是我能想到的最好的结果:

WITH CTE AS 
(SELECT T1.linenum
    , word = STUFF((
          SELECT ' ' + T2.wordtext
          FROM TableName T2
          WHERE T1.linenum = T2.linenum
          FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM TableName T1
GROUP BY T1.linenum)
SELECT T3.*
FROM CTE JOIN
TableName T3 ON CTE.linenum=T3.linenum
WHERE CTE.word LIKE '%I love dogs%'

结果:

ID  LINENUM WORDTEXT
6   3       I
7   3       love
8   3       dogs
9   3       too

SQL Fiddle中的示例结果。

答案 1 :(得分:1)

你需要一个分离器功能。阅读Jeff Moden的article,了解其中最快的分割器之一。

首先,您希望将WordText与相同的LineNum连接起来以生成句子,并检查连接的WordText是否包含@sentence参数。然后,您只能从WordTexts表中获得Words中包含@sentence字样的DECLARE @sentence VARCHAR(8000) = 'i love dogs' DECLARE @delimiter CHAR(1) = ' ' ;WITH CTE AS( SELECT w1.LineNum, Sentence = STUFF(( SELECT ' ' + w2.WordText FROM Words w2 WHERE w1.LineNum = w2.LineNum FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 1, '') FROM Words w1 GROUP BY w1.LineNum ) SELECT w.* FROM CTE c INNER JOIN Words w ON w.LineNum = c.LineNum INNER JOIN dbo.DelimitedSplit8K(@sentence, @delimiter) d ON d.Item = w.WordText WHERE c.Sentence LIKE '%' + @sentence + '%' ORDER BY w.ID

ID          LineNum     WordText
----------- ----------- ----------
6           3           i
7           3           love
8           3           dogs

SQL Fiddle

<强> RESULT

{{1}}

答案 2 :(得分:0)

这里我将给定的单词拆分为列并存储到表中。并使用countpartition by对单词进行计数,并通过匹配给定文本的单词计数来加入主表。

我已将此答案用于将文本拆分为表格列。

 declare @commavalue varchar(50)='I love dogs'
 declare @table1 table(id int identity(1,1), wordtext varchar(30))

 insert into @table1

 select q2.value from
 (
   SELECT cast('<x>'+replace(@commavalue,'','</x><x>')+'</x>' as xml) 
   as Data
 ) q1 

 CROSS APPLY

 (
   SELECT x.value('.','varchar(100)') as value 
   FROM Data.nodes('x') as f(x)
 ) q2

declare @table table(id int identity(1,1), linenum int, wordtext varchar(30))

insert into @table values( 1, 'i' )
insert into @table values( 1, 'love')
insert into @table values( 2, 'i' )
insert into @table values( 2, 'love')
insert into @table values( 2, 'ice')
insert into @table values( 3, 'i')
insert into @table values( 3, 'love' )
insert into @table values( 3, 'dogs')
insert into @table values( 3, 'too' )

select t.* from
(
 select t1.linenum, 
 count(t1.wordtext) over(partition by t1.linenum order by t1.id) wordCount
 from @table t1  
 join @table1 t2 on t1.wordtext = t2.wordtext
)
 wc
 join @table t on wc.linenum = t.linenum
 join @table1 t1 on t.wordtext = t1.wordtext
where wordcount = (select count(1) from @table1)