Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找序列字SQL_Sql_Sql Server_Search - Fatal编程技术网

查找序列字SQL

查找序列字SQL,sql,sql-server,search,Sql,Sql Server,Search,我正在使用SQL Server 2008,我需要在单词表中搜索完整的句子 言语 如果句子是“我爱狗”,则本例中的结果应仅为ID 6-8 ID LineNum WordText ----------- ----------- ------------ 6 3 i 7 3 love 8 3 dogs 试试这个: SELECT T1.linenum,

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

言语

如果句子是“我爱狗”,则本例中的结果应仅为ID 6-8

ID          LineNum     WordText
----------- ----------- ------------
6           3           i
7           3           love
8           3           dogs
试试这个:

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
ID  LINENUM WORDTEXT
6   3       I
7   3       love
8   3       dogs
9   3       too
请参阅中的结果

编辑:

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

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%'
结果:

LINENUM     WORD
----------------------------
1           i love
2           i love ice
3           I love dogs too
ID  LINENUM WORDTEXT
6   3       I
7   3       love
8   3       dogs
9   3       too
示例结果。

为此需要一个拆分器函数。阅读杰夫·摩登的《最快的分路器》一书

首先,要将WordText与相同的LineNum连接起来以生成句子,并检查连接的WordText中是否包含@句子参数。然后,您只能从包含@句子中的单词的单词表中获取单词文本

结果


在这里,我将给定的单词拆分为列并存储到表中。通过使用count和partition 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)

如果序列多次出现,您希望所有匹配项都出现吗?您想在句子中复合多个连续空格吗?这里有一个更好的字符串拆分选项与TSQL的比较
 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)