2012-08-12 92 views
-1

我在嘗試將發生在一行中的所有單詞分組,並計數它們的freq值並按計數結果進行排序。它只能在sql中完成。這裏有一個例子:在mysql中進行分組和計數

我有如下表:

word | freq 
----------- 
car | 5 
cake | 2 
car | 4 

,並希望得到這樣的結果:

word | freq 
----------- 
car | 9 
cake | 2 
+4

RTFM? http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html – 2012-08-12 18:38:14

回答

2

您可以通過組詞,後來根據製作組來總結.. ...

SELECT word,SUM(freq) AS freq FROM tblName GROUP BY word 

當你必須根據freq命令單詞時,你可以使用ORDER BY in followi NG方式:

SELECT word,SUM(freq) AS freq FROM tblName GROUP BY word ORDER BY freq DESC 
1
SELECT word, SUM(freq) AS freq FROM yourTable GROUP BY word ORDER BY freq DESC;