• A2-02-18.DML-MySQL GROUP BY


    转载自:http://www.mysqltutorial.org/mysql-group-by.aspx

    MySQL GROUP BY

     

    Summary: in this tutorial, you will learn how to use MySQL GROUP BY to group rows into subgroups based on values of columns or expressions.

    Introduction to MySQL GROUP BY clause

    The GROUP BY clause groups a set of rows into a set of summary rows by values of columns or expressions. The GROUP BY clause returns one row for each group. In other words, it reduces the number of rows in the result set.

    You often use the GROUP BY clause with aggregate functions such as SUMAVGMAXMIN, and COUNT. The aggregate function that appears in the SELECT clause provides the information about each group.

    The GROUP BY clause is an optional clause of the SELECT statement. The following illustrates the GROUP BY clause syntax:

    The GROUP BY clause must appear after the FROM and WHERE clauses. Following the GROUP BY keyword is a list of comma-separated columns or expressions that you want to use as criteria to group rows.

    MySQL GROUP BY examples

    Simple MySQL GROUP BY example

    Let’s take a look at the orders table in the sample database.

    orders table

    Suppose you want to group values of the order’s status into subgroups, you use the GROUP BY clause with the status column as the following query:

    Try It Out

    MySQL GROUP BY example

    As you can see, the GROUP BY clause returns unique occurrences of status values. It works like the DISTINCT operator as shown in the following query:

    Try It Out

    MySQL GROUP BY with aggregate functions

    The aggregate functions allow you to perform the calculation of a set of rows and return a single value. The GROUP BY clause is often used with an aggregate function to perform calculation and return a single value for each subgroup.

    For example, if you want to know the number orders in each status, you can use the COUNT function with the GROUP BY clause as follows:

    Try It Out

    MySQL GROUP BY with COUNT function

    See the following orders and  orderdetails table.

    order-orderDetails-tables

    To get the total amount of all orders by status, you join the orders table with the orderdetails table and use the SUM function to calculate total amount. See the following query:

    Try It Out

    MySQL GROUP BY with SUM function

    Similarly, the following query returns the order numbers and the total amount of each order.

    Try It Out

    MySQL GROUP BY order number example

    MySQL GROUP BY with expression example

    In addition to columns, you can group rows by expressions. The following query gets the total sales for each year.

    Try It Out

    MySQL GROUP BY expression example

    In this example, we used the YEAR function to extract year data from order date ( orderDate). We included only orders with shipped status in the total sales. Note that the expression which appears in the SELECT clause must be the same as the one in the GROUP BY clause.

    MySQL GROUP BY with HAVING clause

    To filter the groups returned by GROUP BY clause, you use a  HAVING clause. The following query uses the HAVING clause to select the total sales of the years after 2003.

    Try It Out

    MySQL GROUP BY with HAVING example

    The GROUP BY clause: MySQL vs. standard SQL

    Standard SQL does not allow you to use an alias in the GROUP BY clause, however, MySQL supports this. The following query extracts the year from the order date and counts the orders per year. The year is used as an alias of the expression YEAR(orderDate) and it is used as an alias in the GROUP BY clause too. This query is invalid in standard SQL.

    Try It Out

    MySQL GROUP BY SQL standard

    MySQL also allows you to sort the groups in ascending or descending orders while the standard SQL does not. The default order is ascending. For example, if you want to get the number of orders by status and sort the status in descending order, you can use the GROUP BY clause with DESC as the following query:

    Try It Out

    MySQL GROUP BY DESC example

    Notice that we used DESC in the GROUP BY clause to sort the status in descending order. We could also specify explicitly ASC in the GROUP BY clause to sort the groups by status in ascending order.

    In this tutorial, we have shown you how to use the MySQL GROUP BY clause to group rows into subgroups based on values of columns or expressions.

  • 相关阅读:
    191. Number of 1 Bits
    190. Reverse Bits
    532. K-diff Pairs in an Array
    485. Max Consecutive Ones
    236. Lowest Common Ancestor of a Binary Tree
    235. Lowest Common Ancestor of a Binary Search Tree
    面试题68:树中两个节点的最低公共祖先
    Java—方法重写
    Java—继承
    代码块(Java)
  • 原文地址:https://www.cnblogs.com/zhuntidaoren/p/9517821.html
Copyright © 2020-2023  润新知