zoukankan      html  css  js  c++  java
  • A2-02-19.DML-MySQL HAVING

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

    MySQL HAVING

     

    Summaryin this tutorial, you will learn how to use MySQL HAVING clause to specify a filter condition for groups of rows or aggregates.

    Introduction to MySQL HAVING clause

    The  HAVING clause is used in the SELECT statement to specify filter conditions for a group of rows or aggregates.

    The HAVING clause is often used with the GROUP BY clause to filter groups based on a specified condition. If the GROUP BY clause is omitted, the HAVING clause behaves like the WHERE clause.

    Notice that the HAVING clause applies a filter condition to each group of rows, while the WHERE clause applies the filter condition to each individual row.

    MySQL HAVING clause examples

    Let’s take some examples of using the  HAVING clause to see how it works. We will use the orderdetailstable in the sample database for the demonstration.

    orderdetails table

    You can use GROUP BY clause to get order numbers, the number of items sold per order, and total sales for each:

    Try It Out

    MySQL HAVING - GROUP BY example

    Now, you can find which order has total sales greater than 1000 by using the HAVING clause as follows:

    Try It Out

    MySQL HAVING example

    You can construct a complex condition in the HAVING clause using logical operators such as OR and AND. Suppose you want to find which orders have total sales greater than 1000 and contain more than 600 items, you can use the following query:

    Try It Out

    Suppose you want to find all orders that have shipped and total sales greater than 1500, you can join the orderdetails table with the orders table using the INNER JOIN clause  and apply a condition on statuscolumn and total aggregate as shown in the following query:

    Try It Out

    MySQL HAVING with INNER JOIN example

    The HAVING clause is only useful when you use it with the GROUP BY clause to generate the output of the high-level reports. For example, you can use the HAVING clause to answer statistical questions like finding the number orders this month, this quarter, or this year that have total sales greater than 10K.

    In this tutorial, you have learned how to use the MySQL HAVING clause with the GROUP BY clause to specify filter conditions for groups of rows or aggregates.

  • 相关阅读:
    POJ 1201 Intervals 差分约束
    netframework2.0,asp.net2.0,vs.net 2005
    学习.net第一天
    VS.NET 2003 控件命名规范
    .Net生成共享程序集
    汉字的编码
    [转]用C#实现连接池
    SQL表自连接用法
    一道很好玩的OOP面试题,今天比较有空,所有做了一下
    C#编程规范(2008年4月新版)
  • 原文地址:https://www.cnblogs.com/zhuntidaoren/p/9517898.html
Copyright © 2011-2022 走看看