scripte_php
scripte_php

Reputation: 3

Select duplicate rows

I have data like this :

| col1 |
--------
|  1   |
|  2   |
|  1   |
|  2   |
|  1   |
|  2   |
|  1   |
|  2   |
|  1   |
|  2   |

How can I get like this and order by MAX to Min :

| col1 |
--------
|  2   |
|  1   |

I try this :

SELECT col1 , count(col1 ) FROM myTable GROUP BY col1

But I got strange results

Upvotes: 0

Views: 264

Answers (2)

Jeff Swensen
Jeff Swensen

Reputation: 3573

If you want to order by the count of occurences of each value:

SELECT col1, count(1) FROM myTable GROUP BY col1 ORDER BY count(1) DESC

If you want to order by the actual value contained in col1

SELECT DISTINCT col1 FROM myTable ORDER BY col1 DESC

Upvotes: 2

Samir Talwar
Samir Talwar

Reputation: 14330

You can use the SQL DISTINCT keyword to only show unique results.

SELECT DISTINCT col1 FROM myTable;

You can then order by that column.

SELECT DISTINCT col1 FROM myTable ORDER BY col1 DESC;

Upvotes: 1

Related Questions