Mysql: Order rows by value frequency -


let's have table:

+----+------+---------+ | id | item | country | +----+------+---------+ |  1 | b123 | austria | |  2 | a123 | italy   | |  3 | b990 | germany | |  4 | h231 | austria | |  5 | y233 | france  | |  6 | u223 | austria | |  7 | p022 | spain   | |  8 | d133 | italy   | |  9 | w112 | germany | | 10 | j991 | austria | +----+------+---------+ 

i want select on table , order results country repeated most. expected output should be:

+----+------+---------+ | id | item | country | +----+------+---------+ |  1 | b123 | austria | |  4 | h231 | austria | |  6 | u223 | austria | | 10 | j991 | austria | |  2 | a123 | italy   | |  8 | d133 | italy   | |  3 | b990 | germany | |  9 | w112 | germany | |  5 | y233 | france  | |  7 | p022 | spain   | +----+------+---------+ 

how can that?

i have tried this:

select * items item != '' group item having count(*) > 1 order count(*) desc

but return this:

+----+------+---------+ | id | item | country | +----+------+---------+ |  1 | b123 | austria | |  8 | d133 | italy   | |  3 | b990 | germany | |  5 | y233 | france  | |  7 | p022 | spain   | +----+------+---------+ 

a - original table b - getting counts @ country level. 

by joining , b can sort data in descending order of count display items table.

select a.*   items inner join  (    select country,count(*) cnt              items       item != ''       group item  ) b    on a.country = b.country order b.cnt desc,a.country,a.id;  

Comments