I’m building a blog engine in C# Webforms and I have the following code that selects all blog posts that are current, meaning they should be displayed and a count of all comments for each blog post and it works great.
SELECT PostID, PostTitle, PostDate, PostTeaser, Count(CommentID) AS CountOfCommentID, PostCurrent
FROM TBLBlogPost
INNER JOIN TBLBlogComment ON PostID = PostCommentFK
GROUP BY PostID, PostTitle, PostDate, PostTeaser, PostCurrent
HAVING PostCurrent = 'True'
The problem is it only selects blog posts that have at least one comment.
Does anyone know how I can fix this?
You should use a LEFT JOIN then, instead of the INNER JOIN
INNER JOINmeans that the matching table MUST match or else it will not fill the resultset. ALEFT JOINmeans that if the match is not made, it will fill everything from that table withNULLHere is a good visual representation of the different SQL joins.