在 SQL 列中查找最频繁的值 [英] Find most frequent value in SQL column

查看:25
本文介绍了在 SQL 列中查找最频繁的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 SQL 表的给定列中找到最频繁的值?

How can I find the most frequent value in a given column in an SQL table?

例如,对于这个表,它应该返回 two 因为它是最频繁的值:

For example, for this table it should return two since it is the most frequent value:

one
two
two
three

推荐答案

SELECT
  <column_name>,
  COUNT(<column_name>) AS `value_occurrence` 

FROM
  <my_table>

GROUP BY 
  <column_name>

ORDER BY 
  `value_occurrence` DESC

LIMIT 1;

替换.如果要查看列的 N 个最常见值,请增加 1.

Replace <column_name> and <my_table>. Increase 1 if you want to see the N most common values of the column.

这篇关于在 SQL 列中查找最频繁的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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