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 CASEs within the SUM() call, but this resulted in a fatal error. How can I achieve this?
Thanks!
Seems like what you’re really trying to do is this:
Since you’re only interested in
commentswhosestatusis'approved', the condition should be in join condition.Edit: I have updated the query assuming you want to count comments whose
statusis'approved'andflaggedis equal to0.