在 SQL 列中查找最频繁的值

2021-11-20 00:00:00 sql mysql

如何在 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.

相关文章