MySQL-group BY, having

Source: Internet
Author: User

1. Grouping: GROUP BY statements

Group by must follow the conditions in the WHERE clause before order by (if any), as follows:

Select Column1, Column2  from table1, table2 where Conditionsgroup by Column1, Column2order by Column1, Column2

1) The selected field (the Select field in the query, such as COLUMN1,COLUMN2) can and must appear in the GROUP BY clause, except the summary function , if the field is not found in the Select, cannot be used with the GROUP BY clause. This is logical-how to group the data if it is not displayed.

2) The order of the fields in the GROUP BY clause is not the same as in the SELECT clause, and the groupings are equal, regardless of the order.

2.HAVING clause

The HAVING clause is used in conjunction with select and group by to tell Group by which groupings should be included in the output. Having a role for group by is equivalent to where for select. Therefore, using the HAVING clause allows the result to include or remove the entire set of data.

Having to follow group by before order by

MySQL-group BY, having

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.