如何在SQL表的给定列中找到最常用的值?
例如,对于此表,它应该返回,two因为它是最常见的值:
one
two
two
three
Run Code Online (Sandbox Code Playgroud)
Mih*_*ncu 164
SELECT `column`,
COUNT(`column`) AS `value_occurrence`
FROM `my_table`
GROUP BY `column`
ORDER BY `value_occurrence` DESC
LIMIT 1;
Run Code Online (Sandbox Code Playgroud)
替换column和my_table.1如果要查看列的N最常见值,请增加.
Mat*_*Mat 39
尝试类似的东西:
SELECT `column`
FROM `your_table`
GROUP BY `column`
ORDER BY COUNT(*) DESC
LIMIT 1;
Run Code Online (Sandbox Code Playgroud)
小智 19
让我们将表名tblperson和列名视为city.我想从城市列中检索最重复的城市:
select city,count(*) as nor from tblperson
group by city
having count(*) =(select max(nor) from
(select city,count(*) as nor from tblperson group by city) tblperson)
Run Code Online (Sandbox Code Playgroud)
这nor是别名.
以下查询在SQL Server数据库中似乎对我有用:
select column, COUNT(column) AS MOST_FREQUENT
from TABLE_NAME
GROUP BY column
ORDER BY COUNT(column) DESC
Run Code Online (Sandbox Code Playgroud)
结果:
column MOST_FREQUENT
item1 highest count
item2 second highest
item3 third higest
..
..
Run Code Online (Sandbox Code Playgroud)