Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL order by COUNT DISTINCT

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

like image 341
Isis Avatar asked Sep 24 '10 13:09

Isis


People also ask

Can you use distinct with ORDER BY?

There is no way this query can be executed reasonably. Either DISTINCT doesn't work (because the added extended sort key column changes its semantics), or ORDER BY doesn't work (because after DISTINCT we can no longer access the extended sort key column).

Can I use distinct with count?

Yes, you can use COUNT() and DISTINCT together to display the count of only distinct rows.

How can I count distinct values in MySQL?

To count distinct values, you can use distinct in aggregate function count(). The result i3 tells that we have 3 distinct values in the table.

How do I select distinct and count in SQL?

To count the number of different values that are stored in a given column, you simply need to designate the column you pass in to the COUNT function as DISTINCT . When given a column, COUNT returns the number of values in that column. Combining this with DISTINCT returns only the number of unique (and non-NULL) values.


2 Answers

SELECT
  version,
  COUNT(*) AS num
FROM
  my_table
GROUP BY
  version
ORDER BY
  COUNT(*) DESC
like image 131
Tomalak Avatar answered Oct 06 '22 01:10

Tomalak


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;
like image 30
Alexander Konstantinov Avatar answered Oct 06 '22 01:10

Alexander Konstantinov