17、MySQL的数据分组

group by

使用了group by, 就不必指定要计算和估值的每个组了、系统会自动生成
group by 必须出现在where字句之后,order by字句之前
1、创建分组:vend_id为描述字段,不能为计算字段,后面加上计算字段,group by后必须加上所有的描述字段

mysql> select vend_id, count(*) from products group by vend_id;
+---------+----------+
| vend_id | count(*) |
+---------+----------+
|    1001 |        3 |
|    1002 |        2 |
|    1003 |        7 |
|    1005 |        2 |
+---------+----------+

2、创建摘要with rollup

mysql> select vend_id, count(*) from products group by vend_id with rollup;
+---------+----------+
| vend_id | count(*) |
+---------+----------+
|    1001 |        3 |
|    1002 |        2 |
|    1003 |        7 |
|    1005 |        2 |
| NULL    |       14 |
+---------+----------+

3、分组过滤器 having

mysql> select vend_id, count(*) from products group by vend_id with rollup having count(*) > 2;
+---------+----------+
| vend_id | count(*) |
+---------+----------+
|    1001 |        3 |
|    1003 |        7 |
| NULL    |       14 |
+---------+----------+

4、分组和排序

mysql> select order_num, sum(quantity * item_price) from orderitems group by order_num;
+-----------+----------------------------+
| order_num | sum(quantity * item_price) |
+-----------+----------------------------+
|     20005 | 149.87                     |
|     20006 | 55.00                      |
|     20007 | 1000.00                    |
|     20008 | 125.00                     |
|     20009 | 38.47                      |
+-----------+----------------------------+
mysql> select order_num, sum(quantity * item_price) from orderitems group by order_num order by sum(quantity * item_price);
+-----------+----------------------------+
| order_num | sum(quantity * item_price) |
+-----------+----------------------------+
|     20009 | 38.47                      |
|     20006 | 55.00                      |
|     20008 | 125.00                     |
|     20005 | 149.87                     |
|     20007 | 1000.00                    |
+-----------+----------------------------+
mysql> select order_num, sum(quantity * item_price) from orderitems group by order_num having sum(quantity * item_price) > 100 order by sum(quantity * item_price);
+-----------+----------------------------+
| order_num | sum(quantity * item_price) |
+-----------+----------------------------+
|     20008 | 125.00                     |
|     20005 | 149.87                     |
|     20007 | 1000.00                    |
+-----------+----------------------------+

逆序+desc

mysql> select order_num, sum(quantity * item_price) as title from orderitems group by order_num having title > 100 order by title desc;
+-----------+---------+
| order_num | title   |
+-----------+---------+
|     20007 | 1000.00 |
|     20005 | 149.87  |
|     20008 | 125.00  |
+-----------+---------+

分组函数的执行顺序

select–>>from–>>where–>>group by–>>having–>>order by

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值