我需要在MySQL中的“Text”类型字段中找到某个单词及其频率。实际上,我在Postgres找到了一个解决方案,如下所示:
SELECT word, count(*)
FROM (
SELECT regexp_split_to_table(some_column, '\s') as word
FROM some_table
)
GROUP BY word
我怎样才能在MySQL中做到这一点?
答案 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 |
+-------------------------+---------+--------+