Filtering grouping in MySQL

Source: Internet
Author: User

This article is an excerpt from the MySQL must know

I. Filtering grouping

In addition to using group by grouping data, MySQL also allows filtering of groupings, including which groupings to exclude and which groups are excluded. For example, you might want to list all customers with at least two orders. To arrive at this data, filtering must be based on a complete grouping rather than individual rows

We have seen the role of the WHERE clause, but in this example where the task cannot be done because the where filter specifies rows rather than groupings. In fact, where there is no concept of grouping

So, what does not use where to use? MySQL provides an additional clause for this purpose, which is the HAVING clause. Having is very similar to where. In fact, all the types of where clauses that have been learned so far can be replaced with having. The only difference is where filter rows, and having filter groups

Two. Example

the last line of this SELECT statement adds a HAVING clause that filters those groupings of Count (*) >=2 (more than two orders)

three. The difference between having and where

Here is another way of understanding where the data is filtered before grouping, having the data grouped and filtered. This is an important difference.

Filtering grouping in MySQL

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.