Query tips for [MySQL] sub-statements

Source: Internet
Author: User

First, count the number of rows in the group BY statement

In a GROUP BY statement, if a field statistic function is included (such as: count (), SUM () ... In this case, the statistical function will only work on the group by field, so want to get the final result of the number of rows, can only rely on the return of the results through the program to traverse the count of rows, here is a problem, if the results of the query is too large, which will cost a lot of system resources, using subqueries can solve the problem

SELECT Count  as Count  from (SELECTsumas fromWHERE>1GROUP  by  having > 8900  as a

There are a few points to note:

1. You must specify a statistic field for the function (here is the Count function, specified a.id), cannot be a.*;

2. The child table must be aliased;

[MySQL] Sub-statement query tips

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.