Using GROUP by does not meet the criteria he does not return data, how to solve

Source: Internet
Author: User
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
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.