Am trying to write a query that
- Searches tables Posts and Comments for a string
- Returns matching Posts along with their total comment count if there is a match on either table.
I have:
SELECT Posts.id, Comments.id, Count(DISTINCT Comments.id)
FROM Posts
LEFT JOIN Comments ON Comments.postid = Posts.id
WHERE ( Posts.text LIKE '%test%'
OR ( Comments.text LIKE '%test%'
AND Comments.deleted = false ) )
AND Posts.approved = true
AND Posts.deleted = false
GROUP BY Posts.id
The problem with this query is that the where is filtering my Comments.id count, so instead of returning the total number of comments in the post, it returns the number of comments matching 'test'. How can I achieve what I want in a single query?