从MySQL中的字段中选择最常见的值 [英] Select most common value from a field in MySQL

查看:65
本文介绍了从MySQL中的字段中选择最常见的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有一百万行的表,我该如何从字段中选择最常见的值(该值在表中显示最多)?

I have a table with a million rows, how do i select the most common(the value which appears most in the table) value from a field?

推荐答案

您需要按有趣的列进行分组,并为每个值选择值本身及其出现的行数.

You need to group by the interesting column and for each value, select the value itself and the number of rows in which it appears.

然后是排序(将最常见的值放在第一位)并将结果限制为仅一行的问题.

Then it's a matter of sorting (to put the most common value first) and limiting the results to only one row.

以查询形式:

SELECT column, COUNT(*) AS magnitude 
FROM table 
GROUP BY column 
ORDER BY magnitude DESC
LIMIT 1

这篇关于从MySQL中的字段中选择最常见的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆