views:

206

answers:

3

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!

+1  A: 
SELECT
posts.title
, SUM(IF ((comments.status='approved' AND  comments.flagged = 0),1,0)) 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
dnagirl
This code gives me this error:'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '),1,0) AS commentsCount FROM posts INNER JOIN comments ON comments.postID = p'
Arms
@Arms, I've edited the IF statement to balance parentheses.
dnagirl
This works now, thanks :)I am curious as to if your method is more or less efficient than my own answer that I posted.
Arms
@Arms, preface each SELECT with EXPLAIN to see the query strategy that the db intends to use. Then you'll have a good idea of which is more efficient.
dnagirl
+1  A: 

Seems like what you're really trying to do is this:

SELECT
posts.title
, COUNT(*) AS commentsCount
FROM posts
INNER JOIN comments
ON comments.postID = posts.id
AND comments.status = 'approved'
AND comments.flagged = 0
WHERE
posts.status = ?
GROUP BY
posts.title
ORDER BY
commentsCount DESC
LIMIT 5

Since you're only interested in comments whose status is 'approved', the condition should be in join condition.

Edit: I have updated the query assuming you want to count comments whose status is 'approved' and flagged is equal to 0.

Josh Davis
I need to fetch comments that are approved and their flagged column = 0. I tried your SQL, and got this error message: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN 'approved' WHERE posts.status = ? GROUP BY posts.title ORDER'
Arms
Could you explain what you are actually trying to achieve in your question, please? In plain English. For example "I am trying to get the 5 posts with the most comments whose status is 'approved'" except with the flagged part.
Josh Davis
Except for the COUNT(*), this code works fine. Thanks for the help!
Arms
A: 

Based off of Josh Davis' SQL, here's what worked for me:

SELECT
posts.title
, posts.slug
, COUNT(comments.id) AS commentsCount
FROM posts
INNER JOIN comments
ON comments.postID = posts.id
AND comments.status = 'approved'
AND comments.flagged = 0
WHERE
posts.status = ?
GROUP BY
posts.title
ORDER BY
commentsCount DESC
LIMIT 5

Arms