答案 0 :(得分:0)
使用LEFT JOIN
(甚至获取不匹配的类别)加入这两个表,并使用COUNT
聚合函数和GROUP BY
子句。
<强>查询强>
select t1.`IdCategory` as `Category Id`
, t1.`Name` as `Category Name`
, count(t2.`idQuestion`) as `count`
from `categories` t1
left join `question` t2
on t1.`IdCategory` = t2.`IdCategory`
group by t1.`IdCategory`, t1.`Name`
order by count(t2.`idQuestion`) desc;
<强> Find a demo here 强>
答案 1 :(得分:0)
通过idcategory和sum(1)或counte()加入到表中。