晚上好!我如何COALESCE
按COUNT(*)
查询分组?
SELECT categories.name, COALESCE(COUNT(*), 0) as total FROM questions
INNER JOIN categories
ON questions.categoryid = categories.categoryid
GROUP BY name
ORDER BY total DESC;
我阅读了文档,这是执行此操作的主要解决方案,但它没有使用total = 0
返回类别。
亲切的问候
答案 0 :(得分:1)
count()
的返回值无效,因此您不需要coalesce
SELECT categories.name, COUNT(questions.categoryid) as total
FROM questions
RIGHT OUTER JOIN categories
ON questions.categoryid = categories.categoryid
GROUP BY name
ORDER BY total DESC;