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 筛选 过 ...
随机推荐
- Solr with Apache Tomcat
配置教程 1 http://www.duntuk.com/how-install-apache-solr-46-apache-tomcat-7-use-drupal https://www.gotos ...
- 第六章 mybatis注入映射器
为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象 (DAO)的代码,MyBatis-Spring 提供了一个动态代理的实现:Map ...
- hdu 5038 水题 可是题意坑
http://acm.hdu.edu.cn/showproblem.php?pid=5038 就是求个众数 这个范围小 所以一个数组存是否存在的状态即可了 可是这句话真恶心 If not all ...
- 安装php5.5 mssql扩展报错
./configure 后,直接make可能会出现libtool: link: `php_mssql.lo' is not a valid libtool object 的错误. make clean ...
- 网络电话pjsip Getting Started: Building for Apple iPhone, iPad and iPod Touch
Getting Started: Building for Apple iPhone, iPad and iPod Touch ¶ Getting Started Preparation Get th ...
- Android 设置wifi共享电脑服务器资源
其实win7本身就自带无线热点的功能,按下面的方法设置. 开启windows 7的隐藏功能:虚拟WiFi和SoftAP(即虚拟无线AP),就可以让电脑变成无线路由器,实现共享上网,节省网费和路由器购买 ...
- 谈谈java中的构造函数
from 本篇博文主要是为新手.对java语言感兴趣的人和那些没有系统学习过java基础知识的人进行一个总结,在文章中对构造函数进行了较为详细的说明和讨论,也包含了我个人对于java面向对象中构造函数 ...
- activemq 实战三 了解连接器的URI-Understanding connector URIs
Before discussing the details of connectors and their role in the overall ActiveMQ architecture, it’ ...
- Jetty源码学习-编译Jetty源码二三事
工作小几个月了,JDK基础和web应用框架学的的差不多了,开始学习Jetty源码,费了小半天才编译成功,把自己拆过的坑记录下来. 编译前的环境: MAVEN 3.3.Eclips eLuna Serv ...
- shell出现syntax error near unexpected token `<' 解决方法
最新在看一个shell资料时,按照教材,却出现如下错误,不能运行 如下,简单的脚本: #!/bin/bash cat |while read line do echo $line done < ...