找到遗失的单词

时间:2015-09-22 07:24:13

标签: sql sql-server

您好我正在创建一个模块,以便在SQL

中找到字符串中缺少的单词

如果我有一个字符串

  

"男人,只有一个年轻人,船只,只有成年人"

我想在表tbl_missingwords上匹配这些单词。表格如下

ID  Keyword                 Synonym 
1   One young men only      young men,young adults,adults only
2   One young women only    young women,young adults,adults only
3   Domestic cat            Domestic animals,pats,animal themes
4   Domestic dog            Domestic animals,pats,animal themes
5   Adventure               recreation,persuit
6   Boat                    mode of transport,transport 

然后结果应该是

ID  Keyword                 Synonym 
1   One young men only      young men,young adults
6   Boat                    mode of transport,transport

请帮助在SQL中选择这些缺失的字词。

2 个答案:

答案 0 :(得分:3)

首先创建一个用户定义的函数来分割输入字符串。

功能 - fn_split

CREATE FUNCTION [dbo].[fn_Split](@text varchar(8000), @delimiter varchar(20) = ' ')
RETURNS @Strings TABLE
(   
   position int IDENTITY PRIMARY KEY,
   value varchar(8000)  
)
AS
BEGIN

DECLARE @index int
SET @index = -1

WHILE (LEN(@text) > 0)
BEGIN 
SET @index = CHARINDEX(@delimiter , @text) 
IF (@index = 0) AND (LEN(@text) > 0) 
  BEGIN  
    INSERT INTO @Strings VALUES (@text)
      BREAK 
  END 
IF (@index > 1) 
  BEGIN  
    INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))  
    SET @text = RIGHT(@text, (LEN(@text) - @index)) 
  END 
ELSE
  SET @text = RIGHT(@text, (LEN(@text) - @index))
END
RETURN
END

然后尝试以下sql查询。

<强>查询

declare @str as varchar(max)

set @str='Man,one young men only,boat,adults only'

select ID,min(keyword) as keyword,
case when right(min([Synonym]),1) = ','
then replace(left(min([Synonym]),len(min([Synonym]))-1),',,',',')
else replace(min([Synonym]),',,',',') end as [Synonym]
from
(
    select ID,Keyword,value,replace([Synonym],Value,'') as [Synonym]
    from fn_split(@str,','),
    tbl_missingwords
    where Keyword in (select value from fn_split(@str,','))
)t
group by ID;

SQL Fiddle

答案 1 :(得分:2)

您已经就此问了很多问题。现在,重新设计表结构应该是您的首要任务。无论如何,这是我使用分离器和CROSS APPLY

的解决方案

SQL Fiddle

DECLARE @str VARCHAR(MAX) = 'Man,one young men only,boat,adults only'

DECLARE @tblStr AS TABLE(word VARCHAR(MAX))

INSERT INTO @tblStr
    SELECT Item
    FROM dbo.SplitStrings_XML(@str, ',')

;WITH CteMissingWords(ID, Keyword, Synonym) AS(
    SELECT
        w.ID,
        w.Keyword,
        s.Item
    FROM tbl_missingwords w
    CROSS APPLY dbo.SplitStrings_XML(w.Synonym, ',') s
)
SELECT
    tmw.ID, tmw.Keyword, x.Synonym
FROM tbl_missingwords tmw
CROSS APPLY(
    SELECT STUFF((
        SELECT ',' + cmw.Synonym
        FROM CteMissingWords cmw
        WHERE
            cmw.ID = tmw.ID
            AND cmw.Synonym NOT IN(SELECT word FROM @tblStr)
            AND cmw.Keyword IN(SELECT word FROM @tblStr)
        FOR XML PATH('')
    ), 1, 1, '')
)x(Synonym)
WHERE x.Synonym IS NOT NULL

dbo.SplitStrings_XML的定义已在您上一个问题的one中。