mysql 数据操作 单表查询 having 过滤
SELECT 字段1,字段2... FROM 库名.表名
WHERE 条件
GROUP BY field
HAVING 筛选
ORDER BY field
LIMIT 限制条数
1.首先找到表 库.表名
2. 经过where 过滤条件 找到符合条件记录
3.按照分组归类 只剩下组这个单位
4.剩下只能取 组名和聚合函数 ,having过滤只有分组 和聚合函数
HAVING与WHERE不一样的地方在于
执行优先级从高到低:where > group by > having
#1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是where绝对不能使用聚合函数。 #2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数
having 过滤 是在分组之后进行
having 一定配合聚合函数使用
例如:
报错
分组后无法取到其他字段
因为 id_info 是别名,是运行在having之后的 distinct 字段 在执行 having id_info id_info就不存在, id_info在后面执行 所以这里报错
mysql> select post,count(id) as id_info from employee group by post having id_info >5;
ERROR 1463 (42000): Non-grouping field 'id_info' is used in HAVING clause
mysql> select * from employee having id > 5 ;
ERROR 1463 (42000): Non-grouping field 'id' is used in HAVING clause
正确使用
mysql> select post,count(id) from employee group by post ;
+-----------+-----------+
| post | count(id) |
+-----------+-----------+
| operation | 5 |
| sale | 5 |
| teacher | 6 |
+-----------+-----------+
3 rows in set (0.00 sec) mysql> select post,count(id) from employee group by post having count(id) >5 ;
+---------+-----------+
| post | count(id) |
+---------+-----------+
| teacher | 6 |
+---------+-----------+
1 row in set (0.00 sec)
取 职位名以及每个职位里的员工数 并且把每个职位 里 员工数 大于5 的 职位 取出来
mysql> select post,group_concat(id) from employee group by post ;
+-----------+------------------+
| post | group_concat(id) |
+-----------+------------------+
| operation | 16,15,14,13,12 |
| sale | 11,10,9,8,7 |
| teacher | 6,5,4,3,2,1 |
+-----------+------------------+
3 rows in set (0.00 sec) mysql> select post,group_concat(id) from employee group by post having count(id) >5 ;
+---------+------------------+
| post | group_concat(id) |
+---------+------------------+
| teacher | 6,5,4,3,2,1 |
+---------+------------------+
1 row in set (0.00 sec)
mysql 数据操作 单表查询 having 过滤的更多相关文章
- mysql 数据操作 单表查询 having 过滤 练习
1. 查询各岗位内包含的员工个数小于2的岗位名.岗位内包含员工名字.个数 mysql> select post,group_concat(name),count(id) from employe ...
- mysql 数据操作 单表查询 目录
mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...
- mysql 数据操作 单表查询 where 约束 目录
mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...
- 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 数据操作 单表查询
单表查询的语法 distinct 去重 SELECT 字段1,字段2... FROM 表名 库.表名 WHERE 条件 过滤 符合条件的 GROUP BY field 分组条件 HAVING 筛选 过 ...
- mysql 数据操作 单表查询 group by 聚合函数
强调: 如果我们用unique的字段作为分组的依据,则每一条记录自成一组,这种分组没有意义 多条记录之间的某个字段值相同,该字段通常用来作为分组的依据 如果按照每个字段都是唯一的进行分组,意味着按照这 ...
- mysql 数据操作 单表查询 简单查询 避免重复DISTINCT
创建数据库company create database company charset=utf8; use company; company.employee 员工id id int 姓名 emp_ ...
- mysql 数据操作 单表查询 通过四则运算查询
#通过四则运算查询 FROM employee; AS Annual_salary FROM employee; Annual_salary FROM employee; 查看年薪salary*12 ...
随机推荐
- ueditor1_4_3_3编辑器修改文章
html的body中: <script id="editor" type="text/plain" ></script> js中: // ...
- 上传文件到 Sharepoint 的文档库中和下载 Sharepoint 的文档库的文件到客户端
文件操作应用场景: 如果你的.NET项目是运行在SharePoint服务器上的,你可以直接使用SharePoint服务器端对象模型,用SPFileCollection.Add方法 http://msd ...
- css文字超出自动显示省略号
只针对单行文本有效: 01.针对块状元素 ul li{ width: 180px; text-overflow: ellipsis; white-space: nowrap;/*禁止自动换行*/ ov ...
- eclipse中debug模式不能启动运行,run运行模式却能启动运行!
这个问题我郁闷了好久!问题原因:因为断点太多了,断点冲突了. 解决办法:只要进如deug界面,选择BreakPoints选项,然后清除所有断点,再重新debug启动.问题解决! 希望能帮到遇到此问题的 ...
- iteritems()
iteritems() 是列表的一个方法,用法如下: In [1]: dict1 = {"name": "Jeny", "age": 18, ...
- 第一篇:《UNIX 网络编程 第二版》编译环境的搭建
第一步:搭建基本的编译环境 安装gcc, g++, bulid-essential等编译软件 第二步:下载本书示例源码包 第三步:解压下载到的包并放在用户主目录中 第四步:进入包内并执行以下命令 su ...
- docker nginx mysql
docker run -p 9000:9000 --name myphp -v /docker/www/:/var/www/html/ -v /docker/php/php.ini:/usr/loca ...
- 实现类似printf这样的函数
来源:http://www.vimer.cn/2009/12/cc%E5%AE%9E%E7%8E%B0%E5%A4%9A%E5%8F%82%E6%95%B0%E5%87%BD%E6%95%B0%E7% ...
- 【Android N 7.1.1】 锁屏之上显示Toast
package com.android.systemuirom.keyguard; import android.content.Context; import android.view.Gravit ...
- 单用户模式进入centos
修改root密码----------------单用户模式操作 个人原创博客,转载请注明,否则追究法律责任 author: headsen chen date: 2017-9-30 1,开机后,迅速按 ...