开发者

Order the categorys after most threads

开发者 https://www.devze.com 2023-03-28 06:43 出处:网络
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.

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:

  • idThreads
  • title
  • content
  • category
  • creator
  • votes
  • created

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消