【发布时间】:2011-12-10 01:54:59
【问题描述】:
我有一个类别表,其工作方式类似于 StackOverflow 上的标签。有一些类别,例如 JavaScript,它将是一个顶级类别,也许 jQuery 将是 JavaScript 的一个子类别。
我要查询的是每个类别下有多少条记录(在我的应用程序中它们是“问题”)。
这是我到目前为止的 SQL。 question_categories 只是一个连接表,它有一个problem_id 和一个category_id。
select problems.problem_id , categories.category_id , category_name , count(problems.problem_id) as num_problems , is_top
from problems
left join problem_categories on
problems.problem_id = problem_categories.category_id
left join categories on
problem_categories.category_id = categories.category_id
where is_top = 1;
这只会返回一行。我希望返回 is_top = 1 的记录数(这意味着它是顶级类别)。
我怎样才能改变我的查询来做到这一点?
谢谢!!
【问题讨论】: