比较表中的字符串并比较另一个表中的每个关键字

时间:2016-07-09 04:49:35

标签: sql sql-server

我有两张桌子:

表1包含句子列表:

Sentence
----------------------------------------------------------------
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua 
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur

和表2包含关键字和权重列表: tblKeyword

Keyword                                   Weight
----------------------------------------------------------------
dolor sit                                  1
elit                                       3
foobar                                     10

对于表1中的每个句子,我想得到表2中所有关键字的权重总和,可以在表1的句子中找到。如何在不使用游标的情况下在SQL中执行此操作?

预期结果:

 Result
 ------
 4   (sum of weight for sentence 1)
 6   (sum of weight for sentence 2)
 ...

2 个答案:

答案 0 :(得分:1)

这应该让你开始:

SELECT
  S.sentence
  , SUM(K.weight) AS total_weight
FROM Sentence S
JOIN Keyword K
  ON CHARINDEX(K.keyword, S.sentence) > 0
GROUP BY S.sentence
;

(抱歉:无法访问要验证的SQL Server实例。尝试使用MySQL并将INSTR替换为CHARINDEX。)

如果需要调整/进一步详细说明,请发表评论。

答案 1 :(得分:0)

理想情况下我不推荐光标,但根据您的要求,我找到的唯一选项是光标。请检查一下:

declare @table1 table ( sentences varchar(1000))
declare @table2 table ( keyword varchar(1000) , weight int)

insert into @table1 values 
( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua')
,('Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur' )


insert into @table2 values 
('dolor sit' , 1),('elit' , 3),('foobar' , 10)

/*Declare temp variable*/
declare @tab TABLE ( sentSeqno int,  result int) --to get final result create temporary table and insert temp data

declare @keyword varchar(1000), @weight int , @Recordno int = 0 , @sentences varchar(1000);

/*first cursor for table 1 for iteration of each row*/
DECLARE @cursorouter CURSOR ; 
SET @cursorouter = CURSOR FOR  select * from @table1
OPEN @cursorouter
FETCH NEXT FROM @cursorouter INTO @sentences  --FILL IN CURSOR TO LOOP THROUGH
/* loop first cursor while having data*/
WHILE @@FETCH_STATUS = 0 
    BEGIN

       /*Insert into temp data*/ 
       Set @Recordno = @Recordno + 1    insert into @tab values(@Recordno, 0)

       /*Second cursor for table 2 to for iteration of each row*/
       DECLARE @cursorInner CURSOR ; 
        SET @cursorInner = CURSOR FOR 

        select * from @table2 
        OPEN @cursorInner
        FETCH NEXT
        FROM @cursorInner INTO @keyword , @weight --FILL IN CURSOR TO LOOP THROUGH

        WHILE @@FETCH_STATUS = 0 
            BEGIN
                    /*to find the keyword in a sentence, we start loop and whenever found the keyword, we store its weightage*/
                    Declare @pos int =0
                    Declare @oldpos int = 0 

                    select @pos= patindex('%' + @keyword +'%' , @sentences) 
                    /*While loop for each keyword found in sentence, we store the weightage into temp table*/
                    while @pos > 0 and @oldpos <> @pos
                     begin
                        /*update temp data to store result*/ 
                       update @tab set result = result+ @weight where sentSeqno = @Recordno
                       Select @oldpos = @pos
                       select @pos=patindex(@sentences,Substring(@keyword,@pos + 1,len(@keyword))) + @pos
                    end

            FETCH NEXT
            FROM @cursorInner INTO @keyword , @weight;
        END
        CLOSE @cursorInner;
        DEALLOCATE @cursorInner;

    FETCH NEXT
        FROM @cursorouter INTO @sentences;
    END
    CLOSE @cursorouter;
    DEALLOCATE @cursorouter;

select * from @tab