0

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

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

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

4

1 回答 1

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 |
+-------------------------+---------+--------+
于 2013-10-22T16:50:40.873 回答