开发者

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:

  • 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜