文本字段中的字频率(MySQL)

时间:2013-10-22 16:39:50

标签: mysql mysql-workbench

我需要在MySQL中的“Text”类型字段中找到某个单词及其频率。实际上,我在Postgres找到了一个解决方案,如下所示:

SELECT word, count(*)
FROM ( 
  SELECT regexp_split_to_table(some_column, '\s') as word
  FROM some_table
) 
GROUP BY word 

我怎样才能在MySQL中做到这一点?

1 个答案:

答案 0 :(得分:1)

通常,当这个问题出现时,OP会使用一些应用程序级代码,但粗略地说......

SET @needle = 'the';

SET @haystack = 'the taming of the shrew';

SELECT @haystack, @needle, (LENGTH(@haystack)-LENGTH(REPLACE(@haystack,@needle,'')))/LENGTH(@needle)x;
+-------------------------+---------+--------+
| @haystack               | @needle | x      |
+-------------------------+---------+--------+
| the taming of the shrew | the     | 2.0000 |
+-------------------------+---------+--------+