zoukankan      html  css  js  c++  java
  • 分组聚合

     分组

        group  by   根据谁分组,可以求这个组的最大值,最小值,平均值,求和,但是这个求出来的值只是和分组字段对应

          并不和其他任何字段对应,这个时候查出来的所有其它字段都不生效

    select post,group_concat(emp_name) from employee group by post;

           select   podt,group_concat(emp_name)   from    employee   group   by     post;

    聚合函数  

        

        count     求个数     

              select sex,count(id) from employee group by sex;

         max  求最大值

              select post,max(salary) from employee group by post;

        min   求最小值

              select post,min(salary) from employee group by post;

        sum   求和

        avg   求平均

              select sex,avg(salary) from employee group by sex;

            select sex,count(age) from employee group by sex;

    having   过滤语句

    在having条件中可以使用聚合函数,where中不行。

         select post,avg(salary) from employee group by post having avg(salary)>10000;   平均薪资大于10000的部门

    适合去筛选符合条件的莫一组数据,而不是某一行数据。

    先分组在过滤:求平均薪资大于xx的部门,求人数大于xx的性别,求大于xx人的年龄段。

          select age,count(id) from employee group by age having count(id)>5;

          select sex,count(id) from employee group by sex having count(id)>5;

          select post,group_concat(emp_name),count(id) from employee group by post having count(id)<2;

      根据薪资多少进行排序   order    by  默认是升序(asc)    desc降序

            select * from employee order by salary;   从小到大

            select * from employee order by salary desc;   从大到小

                select * from employee order by age,salary;   根据年龄从小到大排。

            select * from employee order by age,salary;    当年龄相同时,依照薪资从小到大排。

        取出薪资排名最高的人   limit  1

            select emp_name,salary from employee order by salary desc limit 1;

              limit   m,n        limit   m,offset  n   两个一样

                    从m+1项开始,取n项

                    如果不写m,m默认为0

                        select emp_name,salary from employee order by salary desc limit 1,3;     取第2,3,4名的薪资

    查询条件的流程顺序:

    1.执行from   表

    2.执行where条件   根据筛选符合条件的行    select   emp_name   as   name   from   employee    where   name= 'egon';   执行失败  应为select  是在第五步才执行

    3.group    by   分组

    4.having      过滤条件    根据分组之后的内容进行组的过滤

    5.select    字段  

    6.order     by   排序        select emp_name as name,age  as  a from   employee    order    by   a;可以执行

    7.limit      m,n   取从m+1开始的钱n条

    where条件中不能使用select字段的重命名

    order     by后者having可以使用select字段的重命名

        主要是order    by在select语句之后才执行

        having经过了mysql的特殊处理,使得它能够感知到select于语句中的重命名

    扩展   

        select    now( )

            在执行select语句的时候,实际上是通过where,group   by,having这几个语句锁定对应的行,然后循环每一行执行select语句

  • 相关阅读:
    leetcode--Populating Next Right Pointers in Each Node II
    leetcode—Populating Next Right Pointers in Each Node
    Pascal's Triangle II
    leetcode—pascal triangle
    leetcode—triangle
    October 23rd, 2017 Week 43rd Monday
    October 22nd, 2017 Week 43rd Sunday
    October 21st 2017 Week 42nd Saturday
    October 20th 2017 Week 42nd Friday
    October 19th 2017 Week 42nd Thursday
  • 原文地址:https://www.cnblogs.com/ch2020/p/12902197.html
Copyright © 2011-2022 走看看