Search code examples
phpmysqlsqldescendant

How to fetch rows by descending order by appears number?


I'm asking if there are a possibility to order results by a descendant order of number of appearance of rows, an example to clarify: here are my table:

foo
foo1
foo
foo
foo1
foo2

here is my result after a query (that I cant guess) call:

foo 
foo 
foo 
foo1
foo1
foo2

coz foo apears 3 times then foo1 that apears 2 times and then foo2 apears just 1 times.

So, how the query used look like?

I'll appreciate any support! My regards!


Solution

  • select t1.col from
    table as t1 inner join (
    select col,count(col) as num
    from table 
    group by col) as t2
    on t1.col = t2.col
    order by t2.num desc