MySQL数据库之MySQL HAVING介绍
小标 2019-01-04 来源 : 阅读 1299 评论 0

摘要:本文主要向大家介绍了MySQL数据库之MySQL HAVING介绍 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

本文主要向大家介绍了MySQL数据库之MySQL HAVING介绍 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。


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

Introducing MySQL HAVING clause

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

The MySQL HAVING clause is often used with the GROUP BY clause. When using with the GROUP BY clause, you can apply a filter condition to the columns that appear in the GROUP BY clause. If the GROUP BY clause is omitted, the MySQL HAVING clause behaves like the WHERE clause. Notice that the MySQL HAVING clause applies the condition to each group of rows, while the WHERE clause applies the condition to each individual row.

Examples of using MySQL HAVING clause

Let’s take a look at an example of using MySQL HAVING clause.

We will use the orderdetails table in the sample database for the sake of demonstration.


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

SELECT ordernumber,        SUM(quantityOrdered) AS itemsCount,        SUM(priceeach) AS total FROM orderdetails GROUP BY ordernumber


Now, we can find which order has total sales greater than $1000. We use the MySQL HAVING clause on the aggregate as follows:

SELECT ordernumber,        SUM(quantityOrdered) AS itemsCount,        SUM(priceeach) AS total FROM orderdetails GROUP BY ordernumber HAVING total > 1000


We can construct a complex condition in the MySQL HAVING clause using logical operators such as OR and AND. Suppose we want to find which order has total sales greater than $1000 and contains more than 600 items, we can use the following query:

SELECT ordernumber,        sum(quantityOrdered) AS itemsCount,        sum(priceeach) AS total FROM orderdetails GROUP BY ordernumber HAVING total > 1000 AND itemsCount > 600


Suppose we want to find all orders that has shipped and has total sales greater than $1500, we can join the orderdetails table with the orders table by using the INNER JOIN clause, and apply condition on the statuscolumn and the total aggregate as the following query:

SELECT a.ordernumber,        SUM(priceeach) total,        status FROM orderdetails a INNER JOIN orders b ON b.ordernumber = a.ordernumber GROUP BY ordernumber HAVING b.status = 'Shipped' AND              total > 1500;


The MySQL HAVING clause is only useful when we use it with the GROUP BY clause to generate the output of the high-level reports. For example, we can use the MySQL HAVING clause to answer some kinds of queries like give me all the orders in this month, this quarter and this year that have total sales greater than 10K.

In this tutorial, you have learned how to use the MySQL HAVING clause together with the GROUP BY to specify filter condition on groups of records or aggregates.


本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved