Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 775
Fantasim Avatar asked Dec 14 '25 03:12

Fantasim


1 Answers

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
like image 180
Mureinik Avatar answered Dec 16 '25 20:12

Mureinik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!