美文网首页
mysql分组函数

mysql分组函数

作者: 骏龙ll | 来源:发表于2019-03-26 16:03 被阅读0次

一、有5个分组函数

MAX(expr)

MIN(expr)

SUM(expr)

AVG(expr)

COUNT(expr)

最大最小值只能操作

平均值与求和的只能传数值型

分组函数忽略null计算

select count(comm) from emp

select avg(ifnull(comm,0)) from emp

查询员工表中一共有几种岗位类型(去重,distinct)

select count(DISTINCT job)

from emp

注意:

使用分组函数时有严格限制,select子句后面不能再随意写,在group by后出现的才在select后面写,否则语义混乱

分组后条件在having后写

where后不能用分组函数,不能有列别名

oracle中要求select后的列(除分组函数项外)都要写在group by后面,mysql中没有这个要求

--查询每个部门的平均工资,1500以下的员工不算

select deptno,avg(sal)

from emp

where sal>=1500

group by deptno

二、select语句执行过程

书写顺序:select from where group by having order by limit

执行顺序:from where group by having select order by limit

别名是在 where中不可以使用,分组函数要使用别名命名(在别处查询时方便使用,否则会被认为是函数)

不分组不允许单独使用having

三、子查询(嵌套查询)

(无子查询时查比clark工资高的员工信息,如下)

select e.*

from emp e

join emp c

on c.ename='clark'

where e.sal>c.sal

查询不是上级的员工信息--注意null值得影响

select * from emp where empno not in (select mgr from emp where mgr is not null)

--查询部门人数大于所有部门平均人数的部门编号,部门名称,部门人数

-查出每组平均人数select renshu from (select count(empno) renshu from emp group by deptno)

-select d.deptno,dname,count(empno)

from emp e

join dept d

on e.deptno=d

group by e.deptno

having count(empno)>(select renshu from (select count(empno) renshu from emp group by deptno))

--将20部门员工工资上涨10部门平均工资

-update emp set sal=sal+(select avg(sal) from emp where deptno=10) where deptno=20

相关文章

  • MySQL——分组函数、distinct、分组查询、连接查询、子

    MySQL——分组函数、distinct、分组查询、连接查询、子查询 一、分组函数(聚合函数)1、 会自动忽略空值...

  • 1-(2)、MySql——基础部分

    一、MySQL之函数(单行函数、分组函数) 1、概述 调用语法:select函数名(实参列表);分组函数和单行函数...

  • mysql分组函数

    二、分组函数 /*功能:用作统计使用,又称为聚合函数或统计函数或组函数 分类:sum 求和、avg 平均值、max...

  • mysql分组函数

    一、有5个分组函数 MAX(expr) MIN(expr) SUM(expr) AVG(expr) COUNT(e...

  • mysql分组函数

    /*功能:用作统计使用,又称为聚合函数或统计函数或组函数分类:avg平均,sum求和,max最大值,min最小值,...

  • MySQL实战3 分组查询和连接查询

    MySQL实战 目录 1.分组查询 分组函数和前面讲的函数不同在于,前面的对内容本身的处理,而分组函数的主要功能是...

  • mysql 分组函数(统计函数)

  • MySQL实战 目录

    MySQL实战 MySQL实战1 数据库概念介绍MySQL实战2 语法、筛选条件和函数MySQL实战3 分组查询和...

  • MySql(六)分组函数

    AVG () 求平均数 SUM () 求和 COUNT () 计数 MAX () 求最大值 MIN () 最小值 ...

  • hive窗口函数

    一.窗口函数基本概念 Mysql8.0也支持窗口函数,也称为分析函数,窗口函数与分组聚合函数类似,但是每一...

网友评论

      本文标题:mysql分组函数

      本文链接:https://www.haomeiwen.com/subject/qanwvqtx.html