I need to
- Get posts where someone has commented in the past 15 days
- Sort by comment count
- Return the post id along with the total number of comments for relevant posts
This is what I have:
SELECT post.id, COUNT(DISTINCT postcomments.id) as comments
FROM posts
LEFT JOIN postcomments on postcomments.postid = post.id
WHERE postcomment.posted_at >= DATE_SUB(NOW(), INTERVAL 15 DAY)
GROUP BY post.id
HAVING comments > 0
ORDER BY comments DESC
The problem with my query is that my comment count only includes comments posted in the past 15 days, whereas I want the total number of comments for the posts. How can I achieve this in one query?