mySQL query to find the most repeated value

后端 未结 2 1102
说谎
说谎 2020-12-08 12:03

I have a table with several rows, for each of them I need to know the most common value.

Example:

row_1 has

car
boat
car
car
truck
truck
plan         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 12:20

    To get a list of values and the number of their appearances:

    select col_name, count(col_name) c from table
    group by col_name
    order by c desc;
    

    If you want only the most common value:

    select col_name, count(col_name) c from table
    group by col_name
    order by c desc
    limit 1;
    

提交回复
热议问题