MySQL Learning (a) use of-group by

Source: Internet
Author: User

Business Scenario One: Query the main table and bring out the number of child tables associated with the main table

Implementation method: Grouping group by child table foreign key

SQL is as follows:

1 Select 2 main.id ID,3 main.name Name,4 ifnull (Sub.num, ' 0 ') Num5  from 6 Xx_main Main7  Left Join(8      Select Count(1) num,main_id fromXx_subGroup  bymain_id9 ) sub on main.id = sub.main_idTen whereMain.id=#{id}

The results of the operation are as follows:

Id Name Num
89 King 3
90 Meteor 1

Analysis: GROUP BY statements group result sets based on one or more columns, and we can use functions such as count,sum,avg on grouped columns


Concluding remarks: Personal sentiment, lack of place also hope you feel free, thank you!

MySQL Learning (a) use of-group by

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.