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 过滤的更多相关文章

  1. mysql 数据操作 单表查询 having 过滤 练习

    1. 查询各岗位内包含的员工个数小于2的岗位名.岗位内包含员工名字.个数 mysql> select post,group_concat(name),count(id) from employe ...

  2. mysql 数据操作 单表查询 目录

    mysql 数据操作 单表查询 mysql 数据操作 单表查询 简单查询 避免重复DISTINCT mysql 数据操作 单表查询 通过四则运算查询 mysql 数据操作 单表查询 concat()函 ...

  3. mysql 数据操作 单表查询 where 约束 目录

    mysql 数据操作 单表查询 where约束 between and or mysql 数据操作 单表查询 where约束 is null in mysql 数据操作 单表查询 where约束 li ...

  4. mysql 数据操作 单表查询 group by 分组 目录

    mysql 数据操作 单表查询 group by 介绍 mysql 数据操作 单表查询 group by 聚合函数 mysql 数据操作 单表查询 group by 聚合函数 没有group by情况 ...

  5. mysql 数据操作 单表查询 group by 介绍

    group by 是在where 之后运行 在写单表查询语法的时候 应该把group by 写在 where 之后 执行顺序 1.先找到表 from 库.表名 2.按照where 约束条件 过滤你想要 ...

  6. mysql 数据操作 单表查询

    单表查询的语法 distinct 去重 SELECT 字段1,字段2... FROM 表名 库.表名 WHERE 条件 过滤 符合条件的 GROUP BY field 分组条件 HAVING 筛选 过 ...

  7. mysql 数据操作 单表查询 group by 聚合函数

    强调: 如果我们用unique的字段作为分组的依据,则每一条记录自成一组,这种分组没有意义 多条记录之间的某个字段值相同,该字段通常用来作为分组的依据 如果按照每个字段都是唯一的进行分组,意味着按照这 ...

  8. mysql 数据操作 单表查询 简单查询 避免重复DISTINCT

    创建数据库company create database company charset=utf8; use company; company.employee 员工id id int 姓名 emp_ ...

  9. mysql 数据操作 单表查询 通过四则运算查询

    #通过四则运算查询 FROM employee; AS Annual_salary FROM employee; Annual_salary FROM employee; 查看年薪salary*12 ...

随机推荐

  1. ChemDraw教程之怎么连接ChemDraw结构

    将两个独立的ChemDraw结构连接到一起是使用者学习操作ChemDraw绘制窗口内容的基本能力之一.为了进一步了解ChemDraw软件,本教程将具体为您介绍怎么连接ChemDraw结构. 一.化学结 ...

  2. app服务端server端数据库设计

  3. linq在获取部门层级树种的应用

    public string GetNavigationsJson() { AjaxA_NAVIGATIONS ajaxnavigations = new AjaxA_NAVIGATIONS(); IL ...

  4. Java 基本语法----进制、运算符

    进 制 对于整数,有四种表示方式: 二进制:0,1 ,满 2 进 1.以 0b 或 0B 开头.十进制:0-9 ,满 10 进 1. 八进制:0-7 ,满 8 进1. 以数字 0 开头表示.十六进制: ...

  5. shell基础篇(五)条件判断

    写脚本时:有时要判断字符串是否相等,数字测试.这对后面学习的shell语句,循环,条件语句做好基础. 条件判断格式  1. test condition : test命令  2. [ conditio ...

  6. 【RF库Collections测试】Count Values In List

    Name:Count Values In ListSource:Collections <test library>Arguments:[ list_ | value | start=0 ...

  7. oct()

    oct() 用于将一个十进制的整数转换成八进制字符串 In [10]: oct(10) Out[10]: ' In [11]: oct(20) Out[11]: '

  8. beginUpdates和endUpdates-实现UITableView的动画块

    我们在做UITableView的修改,删除,选择时,需要对UITableView进行一系列的动作操作. 这样,我们就会用到 [tableView beginUpdates]; if (newCount ...

  9. 【PHP】通过header发送自定义数据

    发送header: 我们定义了三个参数,token.language.region,放入header发送过去 <?php $url = 'http://www.example.com'; $he ...

  10. 【linux系列】linux防火墙的关闭开启

    即时生效 开启:service iptables start 关闭:service iptables stop 重启后生效 开启:chkconfig iptables on 关闭:chkconfig ...