MySQL order by COUNT DISTINCT

13,054

Solution 1

SELECT
  version,
  COUNT(*) AS num
FROM
  my_table
GROUP BY
  version
ORDER BY
  COUNT(*) DESC

Solution 2

SELECT version, COUNT(*) FROM tablename
GROUP BY version
ORDER BY COUNT(*) DESC;

or, alternate syntax

SELECT version, COUNT(*) FROM tablename
GROUP BY 1
ORDER BY 2 DESC;

Solution 3

select version, count(*) from sometable group by version order by count(*) desc;
Share:
13,054
Isis
Author by

Isis

Updated on July 28, 2022

Comments

  • Isis
    Isis almost 2 years
    2.8.7
    2.8.3
    2.8.2
    2.8.7
    2.8.5
    2.8.7
    2.8.7
    2.8.5
    2.6.0
    2.8.3
    2.6.4
    2.6.3
    2.8.4
    2.8.0
    2.6.3
    2.8.5
    2.8.5
    2.8.5
    2.6.0
    2.8.2
    

    How do I bring a unique value version sorted by the number of these versions?

    At the exit I want to get the following:

    2.8.5 5

    2.8.7 4

    2.6.0 2

    2.6.3 2

    2.8.2 2

    2.8.3 2

    2.8.4 2

    2.6.4 1

    2.8.0 1

    ORDER BY count unique versions))

    Sorry for my bad english