2013-10-22 72 views
0

我需要在MySQL中的「文本」類型字段中找到某個字及其頻率。其實,我發現在Postgres的解決方案如下:文本字段中的字頻(MySQL)

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

我怎麼能在MySQL中做到這一點?

+0

你想要什麼,基本上是相同的只使用'SPLIT_STR'代替regexp_split_to_table的' ' – alfasin

回答

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 | 
+-------------------------+---------+--------+