我有一个mysql表,上面有很多单词。
在上面的例子中" hi"正在发生三次我想要创建一个查询,该查询将查看此表并对大多数发生的单词进行排序。
答案 0 :(得分:1)
您需要使用GROUP BY子句和COUNT
函数。
SELECT word, COUNT(*) count FROM your_table GROUP BY word ORDER BY count DESC LIMIT 0,3;
可能的输出:
+------+-------+
| word | count |
+------+-------+
| hai | 2 |
| Hai. | 1 |
| This | 1 |
+------+-------+
如果您希望MySQL将hai
和Hai.
视为同一个字,则应在分组前删除所有非字母字符。见MySQL: how to remove all non-alpha numeric characters from a string?。然后,基于this answer的方法,它将如下所示:
SELECT LOWER(alphanum(word)) word, COUNT(*) count FROM your_table
GROUP BY LOWER(alphanum(word)) ORDER BY count DESC LIMIT 0,3;
可能的结果:
+------+-------+
| word | count |
+------+-------+
| hai | 3 |
| this | 1 |
| joe | 1 |
+------+-------+
答案 1 :(得分:0)
您需要使用group by
方法编写一个sql语句,该方法将组合在一起。这样的事情应该让你开始
select word, count(word)
from table
group by word
order by count(word) desc