mysql having in_MySQL having子句

在本教程中,您将学习如何使用MySQL HAVING子句为行分组或聚合组指定过滤条件。

MySQL HAVING子句简介

在SELECT语句中使用HAVING子句来指定一组行或聚合的过滤条件。

HAVING子句通常与GROUP BY子句一起使用,以根据指定的条件过滤分组。如果省略GROUP BY子句,则HAVING子句的行为与WHERE子句类似。

请注意,HAVING子句将过滤条件应用于每组分行,而WHERE子句将过滤条件应用于每个单独的行。

MySQL HAVING子句示例

让我们举一些使用HAVING子句的例子来看看它是如何工作。 我们将使用示例数据库(yiibaidb)中的orderdetails表进行演示。

f74bf00bc45e8223ecd5ef83cc4b46ed.png

mysql> desc orderdetails;

+-----------------+---------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-----------------+---------------+------+-----+---------+-------+

| orderNumber | int(11) | NO | PRI | NULL | |

| productCode | varchar(15) | NO | PRI | NULL | |

| quantityOrdered | int(11) | NO | | NULL | |

| priceEach | decimal(10,2) | NO | | NULL | |

| orderLineNumber | smallint(6) | NO | | NULL | |

+-----------------+---------------+------+-----+---------+-------+

5 rows in set

可以使用GROUP BY子句来获取订单号,查看每个订单销售的商品数量和每个销售总额:

SELECT

ordernumber,

SUM(quantityOrdered) AS itemsCount,

SUM(priceeach*quantityOrdered) AS total

FROM

orderdetails

GROUP BY ordernumber;

执行上面查询语句,得到以下结果(部分) -

+-------------+------------+----------+

| ordernumber | itemsCount | total |

+-------------+------------+----------+

| 10100 | 151 | 10223.83 |

| 10101 | 142 | 10549.01 |

| 10102 | 80 | 5494.78 |

| 10103 | 541 | 50218.95 |

| 10104 | 443 | 40206.20 |

| 10105 | 545 | 53959.21 |

| 10106 | 675 | 52151.81 |

| ------- 这里省略了一大波数据 ---------|

| ........ ........... |

| 10421 | 75 | 7639.10 |

| 10422 | 76 | 5849.44 |

| 10423 | 111 | 8597.73 |

| 10424 | 269 | 29310.30 |

| 10425 | 427 | 41623.44 |

+-------------+------------+----------+

326 rows in set

现在,可以通过使用HAVING子句查询(过滤)哪些订单的总销售额大于55000,如下所示:

SELECT

ordernumber,

SUM(quantityOrdered) AS itemsCount,

SUM(priceeach*quantityOrdered) AS total

FROM

orderdetails

GROUP BY ordernumber

HAVING total > 55000;

执行上面查询语句,得到以下结果 -

+-------------+------------+----------+

| ordernumber | itemsCount | total |

+-------------+------------+----------+

| 10126 | 617 | 57131.92 |

| 10127 | 540 | 58841.35 |

| 10135 | 607 | 55601.84 |

| 10142 | 577 | 56052.56 |

| 10165 | 670 | 67392.85 |

| 10181 | 522 | 55069.55 |

| 10192 | 585 | 55425.77 |

| 10204 | 619 | 58793.53 |

| 10207 | 615 | 59265.14 |

| 10212 | 612 | 59830.55 |

| 10222 | 717 | 56822.65 |

| 10287 | 595 | 61402.00 |

| 10310 | 619 | 61234.67 |

| 10312 | 601 | 55639.66 |

| 10390 | 603 | 55902.50 |

+-------------+------------+----------+

可以使用逻辑运算符(如OR和AND)在HAVING子句中构造复杂过滤条件。 假设您想查找哪些订单的总销售额大于50000,并且包含超过600个项目,则可以使用以下查询:

SELECT

ordernumber,

SUM(quantityOrdered) AS itemsCount,

SUM(priceeach*quantityOrdered) AS total

FROM

orderdetails

GROUP BY ordernumber

HAVING total > 50000 AND itemsCount > 600;

执行上面查询语句,得到以下结果 -

+-------------+------------+----------+

| ordernumber | itemsCount | total |

+-------------+------------+----------+

| 10106 | 675 | 52151.81 |

| 10126 | 617 | 57131.92 |

| 10135 | 607 | 55601.84 |

| 10165 | 670 | 67392.85 |

| 10168 | 642 | 50743.65 |

| 10204 | 619 | 58793.53 |

| 10207 | 615 | 59265.14 |

| 10212 | 612 | 59830.55 |

| 10222 | 717 | 56822.65 |

| 10310 | 619 | 61234.67 |

| 10312 | 601 | 55639.66 |

| 10360 | 620 | 52166.00 |

| 10390 | 603 | 55902.50 |

| 10414 | 609 | 50806.85 |

+-------------+------------+----------+

假设您想查找所有已发货(status='Shiped')的订单和总销售额大于55000的订单,可以使用INNER JOIN子句将orders表与orderdetails表一起使用,并在status列和总金额(total)列上应用条件,如以下查询所示:

SELECT

a.ordernumber, status, SUM(priceeach*quantityOrdered) total

FROM

orderdetails a

INNER JOIN

orders b ON b.ordernumber = a.ordernumber

GROUP BY ordernumber, status

HAVING status = 'Shipped' AND total > 5000;

执行上面查询,得到以下结果 -

+-------------+---------+----------+

| ordernumber | status | total |

+-------------+---------+----------+

| 10126 | Shipped | 57131.92 |

| 10127 | Shipped | 58841.35 |

| 10135 | Shipped | 55601.84 |

| 10142 | Shipped | 56052.56 |

| 10165 | Shipped | 67392.85 |

| 10181 | Shipped | 55069.55 |

| 10192 | Shipped | 55425.77 |

| 10204 | Shipped | 58793.53 |

| 10207 | Shipped | 59265.14 |

| 10212 | Shipped | 59830.55 |

| 10222 | Shipped | 56822.65 |

| 10287 | Shipped | 61402.00 |

| 10310 | Shipped | 61234.67 |

| 10312 | Shipped | 55639.66 |

| 10390 | Shipped | 55902.50 |

+-------------+---------+----------+

HAVING子句仅在使用GROUP BY子句生成高级报告的输出时才有用。 例如,您可以使用HAVING子句来回答统计问题,例如在本月,本季度或今年总销售额超过10000的订单。

在本教程中,您已经学习了如何使用具有GROUP BY子句的MySQL HAVING子句为行分组或聚合分组指定过滤器条件。

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值