Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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中的标题搜索,并替换掉noice单词_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

SQL中的标题搜索,并替换掉noice单词

SQL中的标题搜索,并替换掉noice单词,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有两张桌子,先给我们看一下细节 第二个是tbl_NoiceWords 第二个表是噪声表,其中噪声词包含…替换为搜索文本…如果我的搜索文本是这就是那个人,那么确切的搜索文本就是这个人 我想从表NoiceWords中搜索替换为NoiceWords的文本。 请告诉我搜索文本字符串的最佳方式。我想您需要这样的方式: DECLARE @nw TABLE ( sn INT, [key] VARCHAR(100) ) INSERT INTO @nw VALUES ( 1, 'and' ),

我有两张桌子,先给我们看一下细节

第二个是tbl_NoiceWords

第二个表是噪声表,其中噪声词包含…替换为搜索文本…如果我的搜索文本是这就是那个人,那么确切的搜索文本就是这个人

我想从表NoiceWords中搜索替换为NoiceWords的文本。
请告诉我搜索文本字符串的最佳方式。

我想您需要这样的方式:

DECLARE @nw TABLE ( sn INT, [key] VARCHAR(100) )

INSERT  INTO @nw
VALUES  ( 1, 'and' ),
        ( 2, 'on' ),
        ( 3, 'of' ),
        ( 4, 'the' ),
        ( 5, 'view' )


DECLARE @s VARCHAR(100) = 'view This of is the Man';
WITH    cte
          AS ( SELECT   sn ,
                        REPLACE(@s, [key], '') AS s
               FROM     @nw
               WHERE    sn = 1
               UNION ALL
               SELECT   n.sn ,
                        REPLACE(s, n.[key], '') AS s
               FROM     @nw n
                        JOIN cte c ON c.sn + 1 = n.sn
             )
    SELECT TOP 1 @s =
            REPLACE(REPLACE(REPLACE(s, ' ', '[]'), '][', ''), '[]', ' ')
    FROM    cte
    ORDER BY sn DESC
输出:

This is Man
首先,递归地从搜索字符串中删除干扰词,最后使用一个小技巧删除重复的连续空格

然后,您可以按如下方式筛选基表:

SELECT * FROM TableName WHERE Title LIKE '%' + @s + '%' 

你可能想考虑全文搜索吗?我怀疑您也希望在搜索时从基表中删除这些干扰词。这将是非常缓慢的。全文搜索针对此类工作进行了优化。它包括噪音词,停止列表和更多

如果不想使用全文搜索,可以向基表中添加其他列,该列将保存Title中的值,但不包含干扰词,并基于该列进行搜索

但如果您坚持以下是完整的代码:

DECLARE @t TABLE
    (
      SNo INT ,
      Title VARCHAR(100)
    )
INSERT  INTO @t
        ( SNo, Title )
VALUES  ( 1, 'women holding stack  of gifts' ),
        ( 2, 'Rear view of a man playing golf' ),
        ( 3, 'Women holding gifts' ),
        ( 4, 'Women holding gifts' ),
        ( 5, 'Businessman reading a newspaper and smiling' ),
        ( 6, 'Hey This some what of is the Man from Chicago' )

DECLARE @nw TABLE
    (
      sn INT ,
      [key] VARCHAR(100)
    )

INSERT  INTO @nw
VALUES  ( 1, 'and' ),
        ( 2, 'on' ),
        ( 3, 'of' ),
        ( 4, 'the' ),
        ( 5, 'view' ),
        ( 6, 'some' ),
        ( 7, 'what' )
以及守则:

DECLARE @s VARCHAR(100) = 'view This of is the Man';
WITH    cte
          AS ( SELECT   sn ,
                        REPLACE(@s, [key], '') AS s
               FROM     @nw
               WHERE    sn = 1
               UNION ALL
               SELECT   n.sn ,
                        REPLACE(s, n.[key], '') AS s
               FROM     @nw n
                        JOIN cte c ON c.sn + 1 = n.sn
             )
    SELECT TOP 1
            @s = REPLACE(REPLACE(REPLACE(s, ' ', '[]'), '][', ''), '[]', ' ')
    FROM    cte
    ORDER BY sn DESC

;WITH    cte
          AS ( SELECT   t.* ,
                        n.sn ,
                        REPLACE(t.Title, n.[key], '') AS s
               FROM     @t t
                        JOIN @nw n ON sn = 1
               UNION ALL
               SELECT   c.SNo ,
                        c.Title ,
                        n.sn ,
                        REPLACE(c.s, n.[key], '')
               FROM     cte c
                        JOIN @nw n ON n.sn = c.sn + 1
             )
    SELECT  *
    FROM    cte
    WHERE   REPLACE(REPLACE(REPLACE(s, ' ', '[]'), '][', ''), '[]', ' ') LIKE '%' + @s + '%'
以及输出:

SNo Title                                           sn  s
6   Hey This some what of is the Man from Chicago   7   Hey This    is  Man from Chicago

你能添加你的期望结果吗?因为很难理解你到底想要什么?可能与你想要的答案重复。@如果serch文本是“拿着一堆礼物的女人”,那么结果应该是S否1@DanielE. 不这不是我想要的。你能解释一下第二个表格是如何进入“搜索”文本的吗?我也在这样做。。我将用C代码替换字符串,然后创建与您上面提到的相同的逻辑。从TableName中选择*,其中的标题类似“%”++@s++“%”,但没有得到正确的结果。实际上,我对全文搜索一无所知:那么是时候学习了!这并不难。您可以在标题列上添加全文索引,然后使用CONTAINS筛选行…好的,谢谢您的回答,我将尝试将您的代码配置到我的数据库中…完成后会通知您:@Giorgi Nakeuri
DECLARE @s VARCHAR(100) = 'view This of is the Man';
WITH    cte
          AS ( SELECT   sn ,
                        REPLACE(@s, [key], '') AS s
               FROM     @nw
               WHERE    sn = 1
               UNION ALL
               SELECT   n.sn ,
                        REPLACE(s, n.[key], '') AS s
               FROM     @nw n
                        JOIN cte c ON c.sn + 1 = n.sn
             )
    SELECT TOP 1
            @s = REPLACE(REPLACE(REPLACE(s, ' ', '[]'), '][', ''), '[]', ' ')
    FROM    cte
    ORDER BY sn DESC

;WITH    cte
          AS ( SELECT   t.* ,
                        n.sn ,
                        REPLACE(t.Title, n.[key], '') AS s
               FROM     @t t
                        JOIN @nw n ON sn = 1
               UNION ALL
               SELECT   c.SNo ,
                        c.Title ,
                        n.sn ,
                        REPLACE(c.s, n.[key], '')
               FROM     cte c
                        JOIN @nw n ON n.sn = c.sn + 1
             )
    SELECT  *
    FROM    cte
    WHERE   REPLACE(REPLACE(REPLACE(s, ' ', '[]'), '][', ''), '[]', ' ') LIKE '%' + @s + '%'
SNo Title                                           sn  s
6   Hey This some what of is the Man from Chicago   7   Hey This    is  Man from Chicago