sort by rows number where columns value are the same

时间:2016-04-25 09:31:34

标签: sql postgresql select count

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

1 个答案:

答案 0 :(得分:1)

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