Fantasim
Fantasim

Reputation: 976

sort by rows number where columns value are the same

I have this two tables :

categorie :

enter image description here

and another table named "thynk" :

enter image description here

each line in table thynk is linked with a categorie.

Anyway i would like to know if it's possible to select categorie_id which contains most of thynk ? (with postgresql)

So here i should have this result :

 categorie_id
--------------
           0
           5
           3

Upvotes: 3

Views: 57

Answers (1)

Mureinik
Mureinik

Reputation: 311823

You could group by categoire_id, count the results in each group and order accordingly:

SELECT   categorie_id, COUNT(*)
FROM     thynk
GROUP BY categorie_id
ORDER BY 2 DESC

Upvotes: 1

Related Questions