美文网首页
SQL第10课:分组数据(GROUP BY)

SQL第10课:分组数据(GROUP BY)

作者: 周运来就是我 | 来源:发表于2018-08-29 22:30 被阅读7次
创建分组
mysql> select vend_id,
    -> count(*) as num_prods
    -> from Products
    -> group by vend_id;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
| BRS01   |         3 |
| DLL01   |         4 |
| FNG01   |         2 |
+---------+-----------+
3 rows in set (0.00 sec)

mysql> select vend_id, count(*) as num_prods from Products;
+---------+-----------+
| vend_id | num_prods |
+---------+-----------+
| BRS01   |         9 |
+---------+-----------+
1 row in set (0.00 sec)

mysql> select *
    -> from Products;
+---------+---------+---------------------+------------+-----------------------------------------------------------------------+
| prod_id | vend_id | prod_name           | prod_price | prod_desc                                                             |
+---------+---------+---------------------+------------+-----------------------------------------------------------------------+
| BR01    | BRS01   | 8 inch teddy bear   |       5.99 | 8 inch teddy bear, comes with cap and jacket                          |
| BR02    | BRS01   | 12 inch teddy bear  |       8.99 | 12 inch teddy bear, comes with cap and jacket                         |
| BR03    | BRS01   | 18 inch teddy bear  |      11.99 | 18 inch teddy bear, comes with cap and jacket                         |
| BNBG01  | DLL01   | Fish bean bag toy   |       3.49 | Fish bean bag toy, complete with bean bag worms with which to feed it |
| BNBG02  | DLL01   | Bird bean bag toy   |       3.49 | Bird bean bag toy, eggs are not included                              |
| BNBG03  | DLL01   | Rabbit bean bag toy |       3.49 | Rabbit bean bag toy, comes with bean bag carrots                      |
| RGAN01  | DLL01   | Raggedy Ann         |       4.99 | 18 inch Raggedy Ann doll                                              |
| RYL01   | FNG01   | King doll           |       9.49 | 12 inch king doll with royal garments and crown                       |
| RYL02   | FNG01   | Queen doll          |       9.49 | 12 inch queen doll with royal garments and crown                      |
+---------+---------+---------------------+------------+-----------------------------------------------------------------------+
9 rows in set (0.00 sec)

group by 在WHERE 之后 在ORDER BY 之前。WHERE 没有分组的概念,用having过滤分组。

mysql> select cust_id, count(*) as orders
    -> from Orders
    -> group by cust_id
    ->  having count(*) >= 2;
+------------+--------+
| cust_id    | orders |
+------------+--------+
| 1000000001 |      2 |
+------------+--------+
1 row in set (0.01 sec)
mysql> select vend_id,count(*) as num_prod
    -> from Products
    -> where prod_price>=4
    -> group by vend_id
    -> having count(*) >=2;
+---------+----------+
| vend_id | num_prod |
+---------+----------+
| BRS01   |        3 |
| FNG01   |        2 |
+---------+----------+
SELECT 子句的使用顺序

最近在学习数据库的相关知识,想和大家分享一下学习后的相关笔记。
当SELECT语句被DBMS执行时,其子句会按照固定的先后顺序执行

顺序序号 子句关键词 子句功能
1. FROM: 对FROM子句中的前两个表执行笛卡尔积,生成虚拟表VT1。
2. ON: 对VT1应用ON筛选器。只有那些使<join_condition>为真的行才被插入VT2。
3. OUTER(JOIN): 如果指定了OUTER JOIN,保留表中未找到匹配的行将作为外部行添加到VT2,生成VT3。
4. WHERE 对VT3应用WHERE筛选器。只有使<where_condition>为TRUE的行才被插入VT4。
5. GROUP BY: 按GROUP BY 子句中的列列表对VT4中的行分组,生成VT5。
6. CUBE ROLLUP: 把超组插入VT5,生成VT6。
7. HAVING: 对VT6应用HAVING筛选器。只有使<having_condition>为TRUE的组才会被插入VT7。
8. SELECT: 处理SELECT列表,产生VT8。
9. DISTINCT: 将重复的行从VT8中移除,产生VT9。
10. ORDER BY: 将VT9中的行按ORDER BY子句中的列列表排序,生成一个有序表(VC10)。
11. TOP: 从VC10的开始处选择指定数量或比例的行,生成表VT11,并返回给调用者。

如果FROM子句包含两个以上的表,则对上一个联接生成的结果表和下一个表重复执行步骤1到步骤3,直到处理完所有的表为止。

常用子句使用顺序:

顺序序号 子句关键词 子句功能
1 SELECT 从指定表中取出指定列的数据
2 FROM 指定要查询操作的表
3 WHERE 用来规定一种选择查询的标准
4 GROUP BY 对结果集进行分组,常与聚合函数一起使用
5 HAVING 返回选取的结果集中行的数目
6 ORDER BY 指定分组的搜寻条件

相关文章

网友评论

      本文标题:SQL第10课:分组数据(GROUP BY)

      本文链接:https://www.haomeiwen.com/subject/bcpvfftx.html