Mysql queries the number of comments, mysql queries comments

Source: Internet
Author: User

Mysql queries the number of comments, mysql queries comments

As a Tom, this problem has been solved for half a day and is recorded here.

Two tables: Article table and comment table

Article table (article): id

Comment table (comment): id, c_aid

Requirement: query the number of all articles and comments and display them in descending order (0 is returned for the number of comments of articles without comments)

SQL: select. id, ifnull (AC. comment_count, 0) as cont from B _articleinfo a left join (select c_aid, count (1) as comment_count from B _article_comment group by c_aid) AC ON. id = AC. c_aid order by. id desc

Tip: because the number of queried comments does not have a corresponding ing, you need to add attributes to the xml of model and mybatis to get the data.

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.