The group by does not meet the criteria he does not return data
Such as: posts post table
Create TABLE posts (
POSTS_ID,
ForumID, (forum ID)
Posts_title,
Posts_posttime,
)
Comments Comment Form
CREATE TABLE Comments (
COMMENTS_ID,
Postsid,
Comments_content,
Comments_posttime,
)
The result I want is: Read all posts with Forum ID 1 and count the total number of comments for each post.
I use COUNT (comments_id) as commentsnum to comment, but the post is not commented on the comment form, group by ignores
posts_id ForumID Commentsnum
1 1 4
2 1 5
3 1 4
4 1 0
I want to be a post in the comment table when no comments, return 0, or return null also OK, how to get
Share to: more
------Solution--------------------
Select A.posts_id,a.forumid,count (b.comments_id) as Commentsnum
From posts a left join comments B on A.posts_id=b.postsid
GROUP BY B.POSTSID have a.forumid=1