MySQL分组聚合

where 与 having 的区别 & order by

假如我们有一张表,表名为 sales,如下所示:

+----------------+-------------+--------------+---------+----------+---------------+
| transaction_id | customer_id | product_name | price   | quantity | purchase_date |
+----------------+-------------+--------------+---------+----------+---------------+
|              1 |         101 | Laptop       | 1200.00 |        2 | 2024-06-01    |
|              2 |         102 | Phone        |  800.00 |        1 | 2024-06-02    |
|              3 |         101 | Tablet       |  500.00 |        3 | 2024-06-03    |
|              4 |         103 | Laptop       | 1200.00 |        1 | 2024-06-04    |
|              5 |         102 | Headphones   |  100.00 |        2 | 2024-06-05    |
|              6 |         101 | Phone        |  800.00 |        1 | 2024-06-06    |
|              7 |         104 | Tablet       |  500.00 |        2 | 2024-06-07    |
|              8 |         102 | Laptop       | 1200.00 |        1 | 2024-06-08    |
|              9 |         101 | Phone        |  800.00 |        2 | 2024-06-09    |
|             10 |         103 | Headphones   |  100.00 |        1 | 2024-06-10    |
+----------------+-------------+--------------+---------+----------+---------------+

having子句

having 子句用于在 SQL 查询中对分组后的结果进行筛选,用于过滤分组后的数据。

where 子句对行数据进行筛选,用于过滤行数据。

  • 想要找出购买商品数量大于 1 且总价值超过 1000 的客户。我们可以这样做:

    select customer_id, sum(price * quantity) as total_purchase_value
    from sales
    group by customer_id
    having sum(quantity) > 1 and sum(price * quantity) > 1000;
    
    +-------------+----------------------+
    | customer_id | total_purchase_value |
    +-------------+----------------------+
    |         101 |              6300.00 |
    |         102 |              2200.00 |
    |         103 |              1300.00 |
    +-------------+----------------------+
    3 rows in set (0.00 sec)
    

    在这个例子中只有满足这两个条件的客户才会包含在结果中。并且像上面having后面的就是分组后的数据。

  • 查找购买了至少三种不同产品的客户。我们可以这样查询:

    select customer_id, count(distinct product_name) as distinct_products_count
    from sales
    group by customer_id
    having count(distinct product_name) >= 3;
    
    +-------------+-------------------------+
    | customer_id | distinct_products_count |
    +-------------+-------------------------+
    |         101 |                       3 |
    |         102 |                       3 |
    +-------------+-------------------------+
    2 rows in set (0.00 sec)
    

这里关于where就不在进行举例子说明了。

如果wherehaving都能输出我们想要的结果,那我们就首选where会提高我们查询的效率。

order by 子句

order by 子句用于在 SQL 查询结果中指定排序顺序。它允许按照一个或多个列的值对查询结果进行排序,默认情况下,order by会按照升序(asc)排列数据,但也可以指定降序(desc)。

以下是 order by 子句的一般语法结构:

select 列名1, 列名2, ...
from 表名
order by 列名1 [asc|desc], 列名2 [asc|desc], ...;
  • 按客户ID和销售额排序

    select customer_id, sum(price * quantity) as total_sales
    from sales
    group by customer_id
    order by customer_id asc, total_sales desc;
    
    +-------------+-------------+
    | customer_id | total_sales |
    +-------------+-------------+
    |         101 |     6300.00 |
    |         102 |     2200.00 |
    |         103 |     1300.00 |
    |         104 |     1000.00 |
    +-------------+-------------+
    4 rows in set (0.00 sec)
    

    在这个例子中,order by customer_id asc, total_sales desc 指定了首先按客户ID升序排序,然后在同一客户ID内按总销售额降序排序。

  • 按产品名称排序

    select product_name, sum(quantity) as total_quantity_sold
    from sales
    group by product_name
    order by product_name asc;
    
    +--------------+---------------------+
    | product_name | total_quantity_sold |
    +--------------+---------------------+
    | Headphones   |                   3 |
    | Laptop       |                   4 |
    | Phone        |                   4 |
    | Tablet       |                   5 |
    +--------------+---------------------+
    4 rows in set (0.00 sec)
    

    在这个例子中,order by product_name asc 指定了按产品名称的字母顺序进行升序排序。

  • 按销售数量和单价排序

    select product_name, sum(quantity) as total_quantity, avg(price) as average_price
    from sales
    group by product_name
    order by total_quantity desc, average_price desc;
    
    +--------------+----------------+---------------+
    | product_name | total_quantity | average_price |
    +--------------+----------------+---------------+
    | Tablet       |              5 |    500.000000 |
    | Laptop       |              4 |   1200.000000 |
    | Phone        |              4 |    800.000000 |
    | Headphones   |              3 |    100.000000 |
    +--------------+----------------+---------------+
    4 rows in set (0.00 sec)
    

    在这个例子中,order by total_quantity desc, average_price desc 指定了按销售数量降序排序,然后在相同销售数量的情况下按平均单价降序排序。

    order by 子句进行排序时,可以根据需要指定列名、表达式、聚合函数或列的别名作为排序的依据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值