Differences between order by, group by, and having in Mysql

Source: Internet
Author: User


In English, order by is the sort of rows. The default value is ascending. Order by must be followed by multiple field names. Group by is a group in English. You must have an aggregate function. At least one group flag field is required for use. Www.2cto.com what is "aggregate function "? For example, sum (), count (), and avg () are all Aggregate functions. The purpose of using group by is to classify and summarize data. For example, select unit name, count (employee id), sum (employee salary) the running result of form [A table] group by unit name is to count the number of employees and total wages of each unit based on the "unit name. In the order in which the SQL command format is used, group by is prior to order. The standard format of the select command is as follows: SELECT select_list [INTO new_table] FROM table_source [WHERE search_condition] [group by group_by_expression] [HAVING search_condition] www.2cto.com 1. group by is a GROUP query. Generally, group BY is used with the aggregate function to use GROUP by. It means that all columns after the select statement do not use the aggregate function, it must appear after group by (important) for example, there are the following database tables: a B 1 abc 1 bcd 1 asdfg if there are the following query statements (This statement is incorrect, for the reason, see the previous principles.) The intention of the select A, B from table group by A query statement is to get the following results (of course, only A B abc 1 bcd asdfg how to change the right three lines into one, so we need to use the aggregate function, as shown below (the correct syntax): select A, count (B) the result of the as quantity from table group by A is that the number of www.2cto.com A is 1 3 2. the Having where clause is used to remove rows that do not meet the where condition before grouping the query results. That is, data is filtered before grouping. The condition cannot contain clustering functions, use the where condition to display specific rows. The having clause is used to filter groups that meet the conditions. That is, data is filtered after the group. conditions often contain clustering functions and the having condition is used to display specific groups, you can also use multiple grouping standards for grouping. The having clause is limited to columns and aggregate expressions defined in the SELECT statement. Generally, you need to repeat the aggregate function expression in the HAVING clause to reference the aggregate value, just as you did in the SELECT statement. Example: select a count (B) from table group by a having count (B)> 2 BY huangxia

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.