mysql 数据操作 单表查询 group by 聚合函数
强调:
如果我们用unique的字段作为分组的依据,则每一条记录自成一组,这种分组没有意义
多条记录之间的某个字段值相同,该字段通常用来作为分组的依据
如果按照每个字段都是唯一的进行分组,意味着按照这个表有多少条记录 就分多少组。没有意义
分组一定是 是 好多条记录 能够按照某个字段 只归为几类进行操作
四 聚合函数
max 最大值
min 最小值
avg 平均值
sum 求和
count 总数个数
# 需求 每个职位有多少个员工
没有where就不用写 对每个组进行 聚合函数的 统计 count
mysql> select count(id) from employee group by post;
+-----------+
| count(id) |
+-----------+
| 5 |
| 5 |
| 6 |
+-----------+
3 rows in set (0.01 sec)
执行顺序:
1.先找到表 from employee 没有过滤条件
2.进行分组 对职位分组
3.交给 select count(id) 统计每组的职位 id个数
mysql> select post,count(id) as emp_count from employee group by post;
+-----------+-----------+
| post | emp_count |
+-----------+-----------+
| operation | 5 |
| sale | 5 |
| teacher | 6 |
+-----------+-----------+
3 rows in set (0.00 sec)
# 取每个职位的最大工资
mysql> select post,max(salary) as emp_max from employee group by post;
+-----------+------------+
| post | emp_max |
+-----------+------------+
| operation | 20000.00 |
| sale | 4000.33 |
| teacher | 1000000.31 |
+-----------+------------+
3 rows in set (0.13 sec)
# 取每个职位的最小工资
mysql> select post,min(salary) as emp_min from employee group by post;
+-----------+----------+
| post | emp_min |
+-----------+----------+
| operation | 10000.13 |
| sale | 1000.37 |
| teacher | 2100.00 |
+-----------+----------+
3 rows in set (0.00 sec)
# 取每个职位的平均工资
mysql> select post,avg(salary) as emp_avg from employee group by post;
+-----------+---------------+
| post | emp_avg |
+-----------+---------------+
| operation | 16800.026000 |
| sale | 2600.294000 |
| teacher | 175766.718333 |
+-----------+---------------+
3 rows in set (0.00 sec)
# 取每个职位的年龄的总和
mysql> select post,sum(age) as emp_avg from employee group by post;
+-----------+---------+
| post | emp_avg |
+-----------+---------+
| operation | 100 |
| sale | 150 |
| teacher | 263 |
+-----------+---------+
3 rows in set (0.00 sec)
#强调:聚合函数聚合的是组的内容,若是没有分组,则默认一组
示例:
SELECT COUNT(*) FROM employee;
SELECT COUNT(*) FROM employee WHERE depart_id=1;
SELECT MAX(salary) FROM employee;
SELECT MIN(salary) FROM employee;
SELECT AVG(salary) FROM employee;
SELECT SUM(salary) FROM employee;
SELECT SUM(salary) FROM employee WHERE depart_id=3;
mysql 数据操作 单表查询 group by 聚合函数的更多相关文章
- mysql 数据操作 单表查询 group by 聚合函数 没有group by情况下
聚合函数只能用在组里使用 #没有group by 则默认算作一组 取出所有员工的最高工资 mysql> select max(salary) from employee; +---------- ...
- mysql 数据操作 单表查询 group by group_concat() 函数
# group_concat() 和concat() 一样拼接字符串 用在分组里 需求:查看每个职位都有哪些员工名字 把所有员工成员的名字都列出来 把每个职位里的员工姓名列出来 mysql> s ...
- mysql 数据操作 单表查询 group by 分组 目录
mysql 数据操作 单表查询 group by 介绍 mysql 数据操作 单表查询 group by 聚合函数 mysql 数据操作 单表查询 group by 聚合函数 没有group by情况 ...
- mysql 数据操作 单表查询 group by 介绍
group by 是在where 之后运行 在写单表查询语法的时候 应该把group by 写在 where 之后 执行顺序 1.先找到表 from 库.表名 2.按照where 约束条件 过滤你想要 ...
- mysql 数据操作 单表查询 group by 注意
GROUP BY 单独使用GROUP BY关键字分组 SELECT post FROM employee GROUP BY post; 注意:我们按照post字段分组,那么select查询的字段只能是 ...
- mysql 数据操作 单表查询 group by 练习
小练习: 1. 查询岗位名以及岗位包含的所有员工名字 mysql> select post,group_concat(name) from employee group by post ; +- ...
- mysql 数据操作 单表查询 目录
mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...
- mysql 数据操作 单表查询 where 约束 目录
mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...
- mysql 数据操作 单表查询
单表查询的语法 distinct 去重 SELECT 字段1,字段2... FROM 表名 库.表名 WHERE 条件 过滤 符合条件的 GROUP BY field 分组条件 HAVING 筛选 过 ...
随机推荐
- openstack中nova组件Hypervisors、Floating_ips的全部python API 汇总
感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限.错误之处在所难免,欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- Sharepoint 2013 Workflow Error
问题: (1)提示“reload the page and then start the workflow”错误 (2)提示“Unable to properly communicate with t ...
- jQuery on()方法绑定动态元素的点击事件无响应的解决办法
$('#check_all').on('click' , function(){ alert(1); }); $("#yujinlist").append(html); count ...
- Windows网络编程Internet Gopher了解下
Gopher:中文译“地鼠”,是迪士尼卡通人物之一(谷佛). 英文原义:The Internet Gopher Protocol 中文释义:(RFC-1436)网际Gopher协议 该系统是在明尼苏达 ...
- PHPCMS列表循环序列号自增标签代码
{pc:content action="position"posid="1"num="3"thumb="1"} {php ...
- POJ 1742 Coins(多重背包, 单调队列)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- OpenGL 4.0 GLSL 基础教程概览——VAO和VBO常用操作接口
(一) OpenGL 4.3 最新渲染管线图 从OpenGL 2.0 到 OpenGL 3.0变化非常大,但从OpenGL 3.0 到OpenGL 4.0 变化不是太大. 着色器程序直接运行在GPU ...
- [020]Sencha Ext JS 6.0使用教程2
本节主要以典型例子介绍如何用Sencha Ext JS6.0进行项目开发 入门阶段总是比较难的,掌握了基本操作步骤,使用方法,架构思维,开发起来还是满顺利,开心的,自己又能掌握一门新技术,又能进步,主 ...
- golang 小知识-持续更新中
Golang 中的指针 - Pointer Go 的原生数据类型可以分为基本类型和高级类型,基本类型主要包含 string, bool, int 及 float 系列,高级类型包含 struct,ar ...
- 在Win64系统上动态加载无签名驱动:WIN64LUD
1.WIN64LUD的全称是WIN64 Load Unsigned Driver,功能如其名,在WIN64系统上加载无签名的驱动. 2.支持Windows 7/8/8.1/2008R2/2012/20 ...