【问题标题】:MySQL - How to count word occurrences in article titles [duplicate]MySQL - 如何计算文章标题中的单词出现次数[重复]
【发布时间】:2013-05-22 13:23:09
【问题描述】:

我希望您能帮助计算 MySql 表中每个“id”在标题中出现的单词。

表格Article和ExpectedResult在http://www.sqlfiddle.com/#!9/f985f/1中提供

提前致谢。

【问题讨论】:

  • 。 .你有你要找的单词列表吗?你只是想计算标题中的字数吗?作为一般规则,包括样本数据和期望的结果会使问题变得更好。
  • 据我了解,根据他的预期结果,他想要每个单词以及它们在标题中使用的次数。顺便说一句,我正在等待这个问题的答案,看起来很酷

标签: mysql count find-occurrences


【解决方案1】:

一种方法:

select a.id, 
       w.word, 
       (LENGTH(a.title) - LENGTH(REPLACE(a.title, w.word, ''))) / LENGTH(w.word) `count`
from Articles a join
(select distinct substring_index(substring_index(title,' ',n),' ',-1) word
 from Articles
 cross join 
 (select 1 n union all select 2 union all select 3 union all select 4 union all
  select 5 union all select 6 union all select 7 union all select 8 union all
  select 9 union all select 10 union all select 11 union all select 12) n
) w on concat(' ',a.title,' ') like concat('% ',w.word,' %')

请注意,n 中的值的数量应该是单个记录的title 中可以出现的最大单词数 - 我这里使用了 12,因为它超过了任何记录标题中的最大单词数提供的数据,但可能需要更大的数字集(取决于实际数据)。

SQLFiddle here.

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 2012-12-08
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    相关资源
    最近更新 更多