zoukankan      html  css  js  c++  java
  • 14 分组函数?

    14 分组函数?
        count 计数
        sum 求和
        avg 平均值
        max 最大值
        min 最小值
        
        记住:所有的分组函数都是对“某一组”数据进行操作的。
        
        找出工资总和?
            select sum(sal) from emp;
                +----------+
                | sum(sal) |
                +----------+
                | 29025.00 |
                +----------+
        
        找出最高工资?
            select empno,ename,max(sal) from emp;
                +-------+-------+----------+
                | empno | ename | max(sal) |
                +-------+-------+----------+
                |  7369 | SMITH |  5000.00 |
                +-------+-------+----------+
                
        找出最低工资?
            select empno,ename,min(sal) from emp;
                +-------+-------+----------+
                | empno | ename | min(sal) |
                +-------+-------+----------+
                |  7369 | SMITH |   800.00 |
                +-------+-------+----------+
                
        找出平均工资?
            select round(avg(sal),2) from emp;
                +-------------------+
                | round(avg(sal),2) |
                +-------------------+
                |           2073.21 |
                +-------------------+
                
        找出总人数?
            select count(empno) from emp;
                +--------------+
                | count(empno) |
                +--------------+
                |           14 |
                +--------------+
                
        分组函数一共有5个,分组函数还有另一个名字:多行处理函数。
        多行处理函数的特点:输入多行,最终输出结果是一行。
        
        分组函数自动忽略null。
            select count(comm) from emp;
                +-------------+
                | count(comm) |
                +-------------+
                |           4 |
                +-------------+
                
            select sum(comm) from emp where comm is not null;// 不需要额外添加这个过滤条件。sum函数自动忽略null。
            
            找出工资高于平均工资的员工?
                select avg(sal) from emp; // 平均工资
                    +-------------+
                    | avg(sal)    |
                    +-------------+
                    | 2073.214286 |
                    +-------------+
                
                select ename,sal from emp where sal > avg(sal);// ERROR 1111 (HY000): Invalid use of group function
                思考以上的错误信息:无效的使用了分组函数?
                    原因:sql语句当中有一个语法规则,分组函数不可直接使用在where子句当中。why???
                    怎么解释?
                        因为group by是在where执行之后才会执行的。
                        
                    select
                        ...    5
                    from
                        ...    1
                    where
                        ...    2
                    group by
                        ...    3
                    having
                        ...    4
                    order by
                        ... 6
                    
                count(*) 和 count(具体的某个字段),他们有什么区别?
                
                找出工资高于平均工资的员工?
                    第一步:找出平均工资
                        select avg(sal) from emp;
                            +-------------+
                            | avg(sal)    |
                            +-------------+
                            | 2073.214286 |
                            +-------------+
                    第二步:找出高于平均工资的员工
                        select ename,sal from emp where sal > 2073.214286;
                        +-------+---------+
                        | ename | sal     |
                        +-------+---------+
                        | JONES | 2975.00 |
                        | BLAKE | 2850.00 |
                        | CLARK | 2450.00 |
                        | SCOTT | 3000.00 |
                        | KING  | 5000.00 |
                        | FORD  | 3000.00 |
                        +-------+---------+
                
                子查询:
                select ename,sal from emp where sal > (select avg(sal) from emp);
                    +-------+---------+
                    | ename | sal     |
                    +-------+---------+
                    | JONES | 2975.00 |
                    | BLAKE | 2850.00 |
                    | CLARK | 2450.00 |
                    | SCOTT | 3000.00 |
                    | KING  | 5000.00 |
                    | FORD  | 3000.00 |
                    +-------+---------+
  • 相关阅读:
    索引失效
    laravel记录笔记Laravel 连接数据库、操作数据库的三种方式
    php 基于redis计数器类
    0x02 MySQL SQL基本语句
    0x01 MySQL What's DataBase
    浅析僵尸进程&孤儿进程
    python并发编程&IO模型
    关于python代码是编译执行还是解释执行
    python并发编程&协程
    基于Cpython的 GIL(Global Interpreter Lock)
  • 原文地址:https://www.cnblogs.com/xlwu/p/13639518.html
Copyright © 2011-2022 走看看