MySQL GROUP BY语句

时间:2022-12-18 14:54:31

GROUP BY 语句根据一个或多个列对结果集进行分组

在分组的列上我们可以使用COUNT、SUM、AVG等函数

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

示例演示1:

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for `employee_tbl`
-- ----------------------------
DROP TABLE IF EXISTS `employee_tbl`;
CREATE TABLE `employee_tbl` (
`id` int(11) NOT NULL,
`name` char(10) NOT NULL DEFAULT '',
`date` datetime NOT NULL,
`singin` tinyint(4) NOT NULL DEFAULT '0' COMMENT '登录次数',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `employee_tbl`
-- ----------------------------
BEGIN;
INSERT INTO `employee_tbl` VALUES ('1', '小明', '2016-04-22 15:25:33', '1'), ('2', '小王', '2016-04-20 15:25:47', '3'), ('3', '小丽', '2016-04-19 15:26:02', '2'), ('4', '小王', '2016-04-07 15:26:14', '4'), ('5', '小明', '2016-04-11 15:26:40', '4'), ('6', '小明', '2016-04-04 15:26:54', '2');
COMMIT; SET FOREIGN_KEY_CHECKS = 1;

  

导入成功之后,执行下面的SQL语句:

mysql> set names utf8;
mysql> SELECT * FROM employee_tbl;
+----+--------+---------------------+--------+
| id | name | date | singin |
+----+--------+---------------------+--------+
| 1 | 小明 | 2016-04-22 15:25:33 | 1 |
| 2 | 小王 | 2016-04-20 15:25:47 | 3 |
| 3 | 小丽 | 2016-04-19 15:26:02 | 2 |
| 4 | 小王 | 2016-04-07 15:26:14 | 4 |
| 5 | 小明 | 2016-04-11 15:26:40 | 4 |
| 6 | 小明 | 2016-04-04 15:26:54 | 2 |
+----+--------+---------------------+--------+
6 rows in set (0.00 sec)

接下来我们使用GROUP BY 语句,将数据表按名字(name)进行分组,并统计每个人有多少条记录:

mysql> SELECT name, COUNT(name) FROM   employee_tbl GROUP BY name;
+--------+----------+
| name | COUNT(name) |
+--------+----------+
| 小丽 | 1 |
| 小明 | 3 |
| 小王 | 2 |
+--------+----------+
3 rows in set (0.01 sec)

示例演示2:

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for `employee_info`
-- ----------------------------
DROP TABLE IF EXISTS `employee_info`;
CREATE TABLE `employee_info` (
`id` int(11) NOT NULL,
`name` varchar(20) NOT NULL DEFAULT '',
`dept` varchar(20) NOT NULL DEFAULT '',
`salary` char(10) NOT NULL DEFAULT '0',
`edlevel` int(11) NOT NULL DEFAULT '0',
`hiredate` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of `employee_info`
-- ----------------------------
BEGIN;
INSERT INTO `employee_info` VALUES ('1', '张三', '开发部', '2000', 3, '2009-10-11'), ('2', '李四', '开发部', '2500', 3, '2009-10-01'), ('3', '王五', '设计部', '2600', 5, '2010-10-02'), ('4', '王六', '设计部', '2300', 4, '2010-10-03'),
('5', '马七', '设计部', '2100', 4, '2010-10-06'), ('6', '赵八', '销售部', '3000', 5, '2010-10-05'), ('7', '钱九', '销售部', '3100', 7, '2010-10-07'), ('8', '孙十', '销售部', '3500', 7, '2010-10-06');
COMMIT; SET FOREIGN_KEY_CHECKS = 1;

员工信息表结构和数据如下:

 id  name  dept  salary  edlevel  hiredate
1 张三 开发部 2000 3 2009-10-11
2 李四 开发部 2500 3 2009-10-01
3 王五 设计部 2600 5 2010-10-02
4 王六 设计部 2300 4 2010-10-03
5 马七 设计部 2100 4 2010-10-06
6 赵八 销售部 3000 5 2010-10-05
7 钱九 销售部 3100 7 2010-10-07
8 孙十 销售部 3500 7 2010-10-06

例如:我想列出每个部门最高薪水的结果,执行如下SQL语句:

mysql> SELECT dept, MAX(salary) FROM   employee_info GROUP BY dept;
+--------+----------+
| dept | MAX(salary) |
+--------+----------+
| 开发部 | 2500 |
| 设计部 | 2600 |
| 销售部 | 3500 |
+--------+----------+
3 rows in set (0.01 sec)

例如,查询每个部门总的薪水数:

mysql> SELECT dept, SUM(salary) FROM   employee_info GROUP BY dept;
+--------+----------+
| dept | SUM(salary) |
+--------+----------+
| 开发部 | 4500 |
| 设计部 | 7000 |
| 销售部 | 9600 |
+--------+----------+
3 rows in set (0.00 sec)

将WHERE子句与GROUP BY 子句一起使用

例如:查询公司2010年入职的各个部门每个级别里的最高薪水

mysql> SELECT dept,edlevel MAX(salary) FROM   employee_info GROUP BY dept,edlevel;
+--------+----------+----------+
| dept | edleve |MAX(salary) |
+--------+----------+----------+
| 开发部 | 4 | 2300 |
| 设计部 | 5 | 2600 |
| 销售部 | 5 | 3000 |
| 销售部 | 5 | 3500 |
+--------+----------+----------+
4 rows in set (0.00 sec)

在GROUP BY 子句之后使用HAVING子句,HAVING子句一般跟在GROUP BY之后,再者,HAVING可以用聚合函数,常见的聚合函数有:COUNT、SUM、AVG

例如:寻找员工数超过2个的部门的最高薪水和最低薪水

解析:先去寻找每个部门里面最高薪水和最低薪水

mysql> SELECT dept,MAX(salary),MIN(salary) FROM   employee_info GROUP BY dept;
+--------+----------+----------+
| dept | MAX(salary) |MIN(salary) |
+--------+----------+----------+
| 开发部 | 2500 | 2000 |
| 设计部 | 2600 | 2100 |
| 销售部 | 3500 | 3000 |
+--------+----------+----------+
3 rows in set (0.00 sec)

再去查找部门员工数超过2个的

mysql> SELECT dept,MAX(salary),MIN(salary) FROM   employee_info GROUP BY dept HAVING COUNT(name) > 2;
+--------+----------+----------+
| dept | MAX(salary) |MIN(salary) |
+--------+----------+----------+
| 设计部 | 2600 | 2100 |
| 销售部 | 3500 | 3000 |
+--------+----------+----------+
2 rows in set (0.00 sec)

 

例如:寻找员工平均工资大于3000的部门的最高和最低薪水

首先第一步还是先去查找每个部门的最高和最低薪水,执行SQL语句:

SELECT dept ,MAX(salary),MIN(salary) from employee_info GROUP BY dept;

然后再去查找平均薪水>3000的,执行SQL语句:

SELECT dept ,MAX(salary),MIN(salary) from employee_info GROUP BY dept HAVING AVG(salary) > 3000;