简体   繁体   中英

coalesce count equals 0 group by

Good evening! How do I COALESCE a grouped by COUNT(*) query?

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;

I read the docs and this is the main solution to do it, but it is not returning the categories with total = 0 .

Kind regards

the return of count() cant be null, so you don't need to coalesce it

you probably try to get this?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM