Here's my query so far:
SELECT
posts.title
, SUM(CASE comments.status WHEN 'approved' THEN 1 END) AS commentsCount
FROM posts
INNER JOIN comments
ON comments.postID = posts.id
WHERE
posts.status = ?
GROUP BY
posts.title
ORDER BY
commentsCount DESC
LIMIT 5
I need to have it also check that comment.flagged
= 0 when it gets commentsCount
. I tried adding additional CASE
s within the SUM()
call, but this resulted in a fatal error. How can I achieve this?
Thanks!