-1

我有两个表关键字和帖子

关键字表看起来像

       keyword    weight
         1         0.3
         2         0.5 
         3         0.4 

例如

和张贴表

         post
       i am beautiful
       1, 2, 3
       1 + 3 = 4

如果我想从 post 表中求和重量

也就是说

我想得到结果

     post     total 
    1, 2, 3     1.2
    1 + 3 = 4   0.7

像这样

我应该使用连接子句吗?

 create procedure RankingPost8
 (@concept_name nvarchar(50))
 AS
 declare @k1 nvarchar(200) 
 select @k1 = keyword from keyword where concept_id = 
 (select Concept_ID from concept where @concept_name = Concept_name) 
 Select @K1 = coalesce(@k1 +' ','') + keyword  from keyword
 begin
 select id as Post_ID, post as Txt from Post 
  where freetext (post, @k1) 

 end 
4

2 回答 2

0

一种可能的解决方案是使用游标。

尝试这样的事情:

UPDATE POST 
SET    TOTAL = 0 

DECLARE @keyword INT, 
        @weight  NUMERIC(4, 2) 
DECLARE CURS CURSOR FAST_FORWARD READ_ONLY FOR 
  SELECT * 
  FROM   KEYWORD 

OPEN CURS 

FETCH NEXT FROM CURS INTO @keyword, @weight 

WHILE @@FETCH_STATUS = 0 
  BEGIN 
      UPDATE POST 
      SET    TOTAL = TOTAL + @weight 
      WHERE  CHARINDEX(CAST(@keyword AS VARCHAR(4)), POST) > 0 

      FETCH NEXT FROM CURS INTO @keyword, @weight 
  END 

CLOSE CURS 

DEALLOCATE CURS 

SELECT POST, 
       TOTAL 
FROM   POST 
WHERE  TOTAL > 0 

可以在SQL Fiddle上找到一个工作示例。

在我的示例中,我在 Post 表中添加了一个包含最终值的列。如果您希望它在不同的表中或仅在输出中而不更改原始表,请告诉我。

于 2013-10-08T06:28:22.770 回答
0

你为什么不试试这样的东西?

SELECT post, sum(weight)
FROM Post
INNER JOIN Keyword ON post LIKE '%'+ Convert(varchar,keyword) + '%'
GROUP BY post

在SQL Fiddle上尝试一下。

我留给您为您的参数添加 where 条件:)

希望你喜欢。

于 2013-10-08T09:46:05.840 回答