Victor Bjelkholm
Victor Bjelkholm

Reputation: 2016

Order the categorys after most threads

I have two tables, one for categories and one for threads.
I want to show all my categories ordered by the number of threads in them. The problem is that I have no idea how to achieve this.

My cats-table:

My threads-table:

The category in my threads-table contains the id of the category.

Upvotes: 1

Views: 34

Answers (1)

Derek
Derek

Reputation: 23268

The following query should give you a list of category names and their corresponding thread counts in descending order:

select
    c.name,
    COUNT(*) as cnt
from
    cats c
    left join threads t
        on  c.idCats = t.category
group by
    c.name
order by
    cnt desc

Upvotes: 3

Related Questions