表1 TradeKeywords
Keyword Amount ...表2 News
NewsId Title Content ...需求是统计TradeKeywords表里Keyword在新闻表里出现的次数,单个词 SELECT COUNT(0) FROM News WHERE Title LIKE '%防盗门%',表循环就不会了,还有就是如何更新得到的统计次数到Amount字段中
Keyword Amount ...表2 News
NewsId Title Content ...需求是统计TradeKeywords表里Keyword在新闻表里出现的次数,单个词 SELECT COUNT(0) FROM News WHERE Title LIKE '%防盗门%',表循环就不会了,还有就是如何更新得到的统计次数到Amount字段中
如果关键词在一个新闻中出现了多次,这个要不要算在出现的记录次数里面,如果这个要计算,那就很麻烦了。如果不算,那简单。select Keyword,count(*)
from TradeKeywords join News on charindex(TradeKeywords ,Title)>0
group by Keyword
set Amount=(select count(*)
from TradeKeywords join News on charindex(TradeKeywords ,Title)>0)试试,没有测试
消息 512,级别 16,状态 1,第 1 行
子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
set Amount=(select count(*)
from News on charindex(TradeKeywords ,Title)>0)试试,如果还不行,你可以先把统计结果放到一个临时表中,然后再进行更新