SQL GROUP BY 语句

GROUP BY 语句用于结合聚合函数,根据一个或多个列对结果集进行分组。

 

1. GROUP BY 语法

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

样本数据库

在本教程中,我们将使用 CodeBaoku 样本数据库。

下面是选自 "Websites" 表的数据:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 编程宝库      | http://www.codebaoku.com/  | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
| 7  | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

下面是 "access_log" 网站访问记录表的数据:

+-----+---------+-------+------------+
| aid | site_id | count | date       |
+-----+---------+-------+------------+
|   1 |       1 |    45 | 2016-05-10 |
|   2 |       3 |   100 | 2016-05-13 |
|   3 |       1 |   230 | 2016-05-14 |
|   4 |       2 |    10 | 2016-05-14 |
|   5 |       5 |   205 | 2016-05-14 |
|   6 |       4 |    13 | 2016-05-15 |
|   7 |       3 |   220 | 2016-05-15 |
|   8 |       5 |   545 | 2016-05-16 |
|   9 |       3 |   201 | 2016-05-17 |
+-----+---------+-------+------------+

 

2. GROUP BY 范例

统计 access_log 各个 site_id 的访问量:

SELECT site_id, SUM(access_log.count) AS nums 
FROM access_log GROUP BY site_id;

执行结果:
+---------+----------+
| site_id |   nums   |
+---------+----------+
|   1     |    275   |
|   2     |    10    |
|   3     |    521   |
|   4     |    13    |
|   5     |    750   |
+---------+----------+

 

3. GROUP BY 多表连接范例

下面的 SQL 语句统计有记录的网站的记录数量:

SELECT Websites.name,COUNT(access_log.aid) AS nums FROM access_log 
LEFT JOIN Websites ON access_log.site_id=Websites.id 
GROUP BY Websites.name;

执行结果:
+-----------+----------+
|   name    |   nums   |
+-----------+----------+
| Facebook  |     2    |
| Google    |     2    |
| 微博       |     1    |
| 淘宝       |     1    |
| 编程宝库   |     3    |
+-----------+----------+

HAVING 子句可以筛选分组后的各组数据,而 WHERE 关键字无法与聚合函数一起使用。SQL HAVING 语法:SELECT column_name, aggregate_function(column_name) FROM table_na...