3

how can I have with one query the following: I would like to have from my comments table all the people how have been commenting on a given post_id and than check how many time the user has commented, based on his name. I would like to avoid to have 2 different queries for it

I have been trying the following but won't return to expected result

SELECT comments.*, COUNT(approved.comment_approved) AS has_commented  FROM wp_comments AS comments  
    INNER JOIN wp_comments AS approved 
    ON comments.comment_author = approved.comment_author

WHERE comments.comment_post_ID =14616
GROUP BY comments.comment_content
1
  • Can you tell the expected result vs the current one ? Even better build a sqlfiddle Commented Jun 3, 2013 at 8:31

1 Answer 1

3

Shouldn't you group by post_ID ? (that would return only one line)

SELECT
  comments.*
, COUNT(approved.comment_approved) AS "has_commented"
FROM wp_comments AS comments
JOIN wp_comments AS approved
  ON (comments.comment_author = approved.comment_author)
WHERE comments.comment_post_ID = 14616
GROUP BY comments.comment_post_ID
;

Or do you want one line per "approved" comment ?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.