zoukankan      html  css  js  c++  java
  • MySQL之分组查询(DQL)

    分组函数

    介绍:

      分组函数作用于一组数据,并对一组数据返回一个值,用作统计使用,又称为聚合函数或统计函数或组函数。

    语法:

      SELECT 查询的字段,分组函数 FROMGROUP BY 分组的字段 ORDER BY 排序的字段;

    特点:

      1.可以按单个字段分组。

      2.和分组函数一同查询的字段最好是分组后的字段。

      3.分组筛选

        分组前筛选:

          针对的表:原始表

          位置:group by的前面

          关键字:where

        分组后筛选:

          针对的表:分组后的结果集

          位置:group by的后面

          关键字:having

      4.可以按多个字段分组,字段之间用逗号隔开。

      5.可以支持排序。

      6.having后可以支持别名。

    组函数类型:

      AVG():平均值,一般用于处理数值型,忽略null值。

      COUNT():计数,可以处理任何类型,忽略null值。

      MAX():最大值,可以处理任何类型,忽略null值。

      MIN():最小值,可以处理任何类型,忽略null值。

      SUM():求和,一般用于处理数值型,忽略null值。

    AVG(平均值)和SUM(合计)函数

    介绍:

      可以对数值型数据使用AVGSUM函数。

    示例:

    SELECT AVG(salary),MAX(salary),MIN(salary),SUM(salary) FROM employees WHERE job_id LIKE '%REP%';

    MIN(最小值)和 MAX(最大值)函数

    介绍:

      可以对任意数据类型的数据使用MINMAX函数。

    示例:

    SELECT MIN(hiredate),MAX(hiredate) FROM employees;

    COUNT(计数)函数

    介绍:

      COUNT(*):返回表中记录总数,适用于任意数据类型。

      COUNT(expr):返回expr不为空的记录总数。

    示例:

    SELECT COUNT(*) FROM employees WHERE department_id = 50;
    SELECT COUNT(commission_pct) FROM employees WHERE department_id = 50;

    分组数据GROUP BY

    介绍:

      可以使用GROUP BY子句将表中的数据分成若干组。

    1.在SELECT列表中所有未包含在组函数中的列都应该包含在GROUP BY子句中。

    示例:

    SELECT department_id,AVG(salary) FROM employees GROUP BY department_id;

    2.包含在GROUP BY子句中的列不必包含在SELECT列表中。

    示例:

    SELECT AVG(salary) FROM employees GROUP BY department_id;

    3.使用多个列分组,在GROUP BY子句中包含多个列。

    示例:

    SELECT department_id dept_id, job_id, SUM(salary) FROM employees GROUP BY department_id, job_id;

    非法使用组函数

    介绍:

      1.不能在WHERE子句中使用组函数。

      2.可以在HAVING子句中使用组函数。

    示例:

    SELECT department_id, AVG(salary) FROM employees WHERE AVG(salary) > 8000 GROUP BY department_id;

    过滤分组HAVING

    介绍:

      使用HAVING过滤分组:

        1.行已经被分组。

        2.使用了组函数。

        3.满足HAVING子句中条件的分组将被显示。

    示例:

    SELECT department_id, MAX(salary) FROM employees GROUP BY department_id HAVING MAX(salary)>10000;

    案例讲解

    #1.简单的分组
    #案例1:查询每个工种的员工平均工资
    SELECT AVG(salary),job_id FROM employees GROUP BY job_id;
    #案例2:查询每个位置的部门个数
    SELECT COUNT(*),location_id FROM departments GROUP BY location_id;
    
    
    #2.可以实现分组前的筛选
    #案例1:查询邮箱中包含a字符的每个部门的最高工资
    SELECT MAX(salary),department_id FROM employees WHERE email LIKE '%a%' GROUP BY department_id;
    #案例2:查询有奖金的每个领导手下员工的平均工资
    SELECT AVG(salary),manager_id FROM employees WHERE commission_pct IS NOT NULL GROUP BY manager_id;
    
    
    #3.分组后筛选
    #案例:查询哪个部门的员工个数>5
    #①查询每个部门的员工个数
    SELECT COUNT(*),department_id FROM employees GROUP BY department_id;
    #②筛选刚才①结果
    SELECT COUNT(*),department_id FROM employees GROUP BY department_id HAVING COUNT(*)>5;
    #案例2:每个工种有奖金的员工的最高工资>12000的工种编号和最高工资
    SELECT job_id,MAX(salary) FROM employees WHERE commission_pct IS NOT NULL GROUP BY job_id HAVING MAX(salary)>12000;
    #案例3:领导编号>102的每个领导手下的最低工资大于5000的领导编号和最低工资manager_id>102
    SELECT manager_id,MIN(salary) FROM employees GROUP BY manager_id HAVING MIN(salary)>5000;
    
    
    #4.添加排序
    #案例:每个工种有奖金的员工的最高工资>6000的工种编号和最高工资,按最高工资升序
    SELECT job_id,MAX(salary) m FROM employees WHERE commission_pct IS NOT NULL GROUP BY job_id HAVING m>6000 ORDER BY m;
    
    
    #5.按多个字段分组
    #案例:查询每个工种每个部门的最低工资,并按最低工资降序
    SELECT MIN(salary),job_id,department_id FROM employees GROUP BY department_id,job_id ORDER BY MIN(salary) DESC;
    
    
    #6.简单的使用
    SELECT SUM(salary) FROM employees;
    SELECT AVG(salary) FROM employees;
    SELECT MIN(salary) FROM employees;
    SELECT MAX(salary) FROM employees;
    SELECT COUNT(salary) FROM employees;
    SELECT SUM(salary) 和,AVG(salary) 平均,MAX(salary) 最高,MIN(salary) 最低,COUNT(salary) 个数 FROM employees;
    SELECT SUM(salary) 和,ROUND(AVG(salary),2) 平均,MAX(salary) 最高,MIN(salary) 最低,COUNT(salary) 个数 FROM employees;
    
    
    #7.参数支持哪些类型
    SELECT SUM(last_name) ,AVG(last_name) FROM employees;
    SELECT SUM(hiredate) ,AVG(hiredate) FROM employees;
    SELECT MAX(last_name),MIN(last_name) FROM employees;
    SELECT MAX(hiredate),MIN(hiredate) FROM employees;
    SELECT COUNT(commission_pct) FROM employees;
    SELECT COUNT(last_name) FROM employees;
    
    
    #8.是否忽略null
    SELECT SUM(commission_pct) ,AVG(commission_pct),SUM(commission_pct)/35,SUM(commission_pct)/107 FROM employees;
    SELECT MAX(commission_pct) ,MIN(commission_pct) FROM employees;
    SELECT COUNT(commission_pct) FROM employees;
    SELECT commission_pct FROM employees;
    
    
    #9.和distinct搭配
    SELECT SUM(DISTINCT salary),SUM(salary) FROM employees;
    SELECT COUNT(DISTINCT salary),COUNT(salary) FROM employees;
    
    
    #10、count函数的详细介绍
    SELECT COUNT(salary) FROM employees;
    -- 效率:
    -- MYISAM存储引擎下,COUNT(*)的效率高
    -- INNODB存储引擎下,COUNT(*)和COUNT(1)的效率差不多,比COUNT(字段)要高一些
    SELECT COUNT(*) FROM employees;
    SELECT COUNT(1) FROM employees;
    
    
    #11.和分组函数一同查询的字段有限制
    SELECT AVG(salary),employee_id FROM employees;
    
    
    #12.查询员工最高工资和最低工资的差距(DIFFERENCESELECT MAX(salary)-MIN(salary) DIFFRENCE FROM employees;
    
    
    #13.查询各个管理者手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内
    SELECT MIN(salary),manager_id FROM employees WHERE manager_id IS NOT NULL GROUP BY manager_id HAVING MIN(salary)>=6000;
    
    
    #14.查询所有部门的编号,员工数量和工资平均值,并按平均工资降序
    SELECT department_id,COUNT(*),AVG(salary) a FROM employees GROUP BY department_id ORDER BY a DESC;
    
    
    #15.选择具有各个job_id的员工人数
    SELECT COUNT(*) 个数,job_id FROM employees GROUP BY job_id;
    
    #16.查询各job_id的员工工资的最大值,最小值,平均值,总和,并按job_id升序
    SELECT MAX(salary),MIN(salary),AVG(salary),SUM(salary),job_id FROM employees GROUP BY job_id
    
    
    #17.查询公司员工工资的最大值,最小值,平均值,总和
    SELECT MAX(salary) 最大值,MIN(salary) 最小值,AVG(salary) 平均值,SUM(salary) 和 FROM employees;
    
    
    #18.查询员工表中的最大入职时间和最小入职时间的相差天数(DIFFRENCE)
    SELECT MAX(hiredate) 最大,MIN(hiredate) 最小,(MAX(hiredate)-MIN(hiredate))/1000/3600/24 DIFFRENCE FROM employees;
    SELECT DATEDIFF(MAX(hiredate),MIN(hiredate)) DIFFRENCE FROM employees;
    SELECT DATEDIFF('1995-2-7','1995-2-6');
    
    
    #19.查询部门编号为90的员工个数
    SELECT COUNT(*) FROM employees WHERE department_id = 90;

    测试数据

    #员工表
    CREATE TABLE `employees` (
      `employee_id` int(6) NOT NULL AUTO_INCREMENT,
      `first_name` varchar(20) DEFAULT NULL,
      `last_name` varchar(25) DEFAULT NULL,
      `email` varchar(25) DEFAULT NULL,
      `phone_number` varchar(20) DEFAULT NULL,
      `job_id` varchar(10) DEFAULT NULL,
      `salary` double(10,2) DEFAULT NULL,
      `commission_pct` double(4,2) DEFAULT NULL,
      `manager_id` int(6) DEFAULT NULL,
      `department_id` int(4) DEFAULT NULL,
      `hiredate` datetime DEFAULT NULL,
      PRIMARY KEY (`employee_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    #员工数据
    INSERT INTO employees VALUES ('100', 'Steven', 'K_ing', 'SKING', '515.123.4567', 'AD_PRES', '24000.00', null, null, '90', '1992-04-03 00:00:00');
    INSERT INTO employees VALUES ('101', 'Neena', 'Kochhar', 'NKOCHHAR', '515.123.4568', 'AD_VP', '17000.00', null, '100', '90', '1992-04-03 00:00:00');
    INSERT INTO employees VALUES ('102', 'Lex', 'De Haan', 'LDEHAAN', '515.123.4569', 'AD_VP', '17000.00', null, '100', '90', '1992-04-03 00:00:00');
    INSERT INTO employees VALUES ('103', 'Alexander', 'Hunold', 'AHUNOLD', '590.423.4567', 'IT_PROG', '9000.00', null, '102', '60', '1992-04-03 00:00:00');
    INSERT INTO employees VALUES ('104', 'Bruce', 'Ernst', 'BERNST', '590.423.4568', 'IT_PROG', '6000.00', null, '103', '60', '1992-04-03 00:00:00');
    INSERT INTO employees VALUES ('105', 'David', 'Austin', 'DAUSTIN', '590.423.4569', 'IT_PROG', '4800.00', null, '103', '60', '1998-03-03 00:00:00');
    INSERT INTO employees VALUES ('106', 'Valli', 'Pataballa', 'VPATABAL', '590.423.4560', 'IT_PROG', '4800.00', null, '103', '60', '1998-03-03 00:00:00');
    INSERT INTO employees VALUES ('107', 'Diana', 'Lorentz', 'DLORENTZ', '590.423.5567', 'IT_PROG', '4200.00', null, '103', '60', '1998-03-03 00:00:00');
    INSERT INTO employees VALUES ('108', 'Nancy', 'Greenberg', 'NGREENBE', '515.124.4569', 'FI_MGR', '12000.00', null, '101', '100', '1998-03-03 00:00:00');
    INSERT INTO employees VALUES ('109', 'Daniel', 'Faviet', 'DFAVIET', '515.124.4169', 'FI_ACCOUNT', '9000.00', null, '108', '100', '1998-03-03 00:00:00');
    INSERT INTO employees VALUES ('110', 'John', 'Chen', 'JCHEN', '515.124.4269', 'FI_ACCOUNT', '8200.00', null, '108', '100', '2000-09-09 00:00:00');
    INSERT INTO employees VALUES ('111', 'Ismael', 'Sciarra', 'ISCIARRA', '515.124.4369', 'FI_ACCOUNT', '7700.00', null, '108', '100', '2000-09-09 00:00:00');
    INSERT INTO employees VALUES ('112', 'Jose Manuel', 'Urman', 'JMURMAN', '515.124.4469', 'FI_ACCOUNT', '7800.00', null, '108', '100', '2000-09-09 00:00:00');
    INSERT INTO employees VALUES ('113', 'Luis', 'Popp', 'LPOPP', '515.124.4567', 'FI_ACCOUNT', '6900.00', null, '108', '100', '2000-09-09 00:00:00');
    INSERT INTO employees VALUES ('114', 'Den', 'Raphaely', 'DRAPHEAL', '515.127.4561', 'PU_MAN', '11000.00', null, '100', '30', '2000-09-09 00:00:00');
    INSERT INTO employees VALUES ('115', 'Alexander', 'Khoo', 'AKHOO', '515.127.4562', 'PU_CLERK', '3100.00', null, '114', '30', '2000-09-09 00:00:00');
    INSERT INTO employees VALUES ('116', 'Shelli', 'Baida', 'SBAIDA', '515.127.4563', 'PU_CLERK', '2900.00', null, '114', '30', '2000-09-09 00:00:00');
    INSERT INTO employees VALUES ('117', 'Sigal', 'Tobias', 'STOBIAS', '515.127.4564', 'PU_CLERK', '2800.00', null, '114', '30', '2000-09-09 00:00:00');
    INSERT INTO employees VALUES ('118', 'Guy', 'Himuro', 'GHIMURO', '515.127.4565', 'PU_CLERK', '2600.00', null, '114', '30', '2000-09-09 00:00:00');
    INSERT INTO employees VALUES ('119', 'Karen', 'Colmenares', 'KCOLMENA', '515.127.4566', 'PU_CLERK', '2500.00', null, '114', '30', '2000-09-09 00:00:00');
    INSERT INTO employees VALUES ('120', 'Matthew', 'Weiss', 'MWEISS', '650.123.1234', 'ST_MAN', '8000.00', null, '100', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('121', 'Adam', 'Fripp', 'AFRIPP', '650.123.2234', 'ST_MAN', '8200.00', null, '100', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('122', 'Payam', 'Kaufling', 'PKAUFLIN', '650.123.3234', 'ST_MAN', '7900.00', null, '100', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('123', 'Shanta', 'Vollman', 'SVOLLMAN', '650.123.4234', 'ST_MAN', '6500.00', null, '100', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('124', 'Kevin', 'Mourgos', 'KMOURGOS', '650.123.5234', 'ST_MAN', '5800.00', null, '100', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('125', 'Julia', 'Nayer', 'JNAYER', '650.124.1214', 'ST_CLERK', '3200.00', null, '120', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('126', 'Irene', 'Mikkilineni', 'IMIKKILI', '650.124.1224', 'ST_CLERK', '2700.00', null, '120', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('127', 'James', 'Landry', 'JLANDRY', '650.124.1334', 'ST_CLERK', '2400.00', null, '120', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('128', 'Steven', 'Markle', 'SMARKLE', '650.124.1434', 'ST_CLERK', '2200.00', null, '120', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('129', 'Laura', 'Bissot', 'LBISSOT', '650.124.5234', 'ST_CLERK', '3300.00', null, '121', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('130', 'Mozhe', 'Atkinson', 'MATKINSO', '650.124.6234', 'ST_CLERK', '2800.00', null, '121', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('131', 'James', 'Marlow', 'JAMRLOW', '650.124.7234', 'ST_CLERK', '2500.00', null, '121', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('132', 'TJ', 'Olson', 'TJOLSON', '650.124.8234', 'ST_CLERK', '2100.00', null, '121', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('133', 'Jason', 'Mallin', 'JMALLIN', '650.127.1934', 'ST_CLERK', '3300.00', null, '122', '50', '2004-02-06 00:00:00');
    INSERT INTO employees VALUES ('134', 'Michael', 'Rogers', 'MROGERS', '650.127.1834', 'ST_CLERK', '2900.00', null, '122', '50', '2002-12-23 00:00:00');
    INSERT INTO employees VALUES ('135', 'Ki', 'Gee', 'KGEE', '650.127.1734', 'ST_CLERK', '2400.00', null, '122', '50', '2002-12-23 00:00:00');
    INSERT INTO employees VALUES ('136', 'Hazel', 'Philtanker', 'HPHILTAN', '650.127.1634', 'ST_CLERK', '2200.00', null, '122', '50', '2002-12-23 00:00:00');
    INSERT INTO employees VALUES ('137', 'Renske', 'Ladwig', 'RLADWIG', '650.121.1234', 'ST_CLERK', '3600.00', null, '123', '50', '2002-12-23 00:00:00');
    INSERT INTO employees VALUES ('138', 'Stephen', 'Stiles', 'SSTILES', '650.121.2034', 'ST_CLERK', '3200.00', null, '123', '50', '2002-12-23 00:00:00');
    INSERT INTO employees VALUES ('139', 'John', 'Seo', 'JSEO', '650.121.2019', 'ST_CLERK', '2700.00', null, '123', '50', '2002-12-23 00:00:00');
    INSERT INTO employees VALUES ('140', 'Joshua', 'Patel', 'JPATEL', '650.121.1834', 'ST_CLERK', '2500.00', null, '123', '50', '2002-12-23 00:00:00');
    INSERT INTO employees VALUES ('171', 'William', 'Smith', 'WSMITH', '011.44.1343.629268', 'SA_REP', '7400.00', '0.15', '148', '80', '2014-03-05 00:00:00');
    INSERT INTO employees VALUES ('172', 'Elizabeth', 'Bates', 'EBATES', '011.44.1343.529268', 'SA_REP', '7300.00', '0.15', '148', '80', '2014-03-05 00:00:00');
    INSERT INTO employees VALUES ('173', 'Sundita', 'Kumar', 'SKUMAR', '011.44.1343.329268', 'SA_REP', '6100.00', '0.10', '148', '80', '2014-03-05 00:00:00');
    INSERT INTO employees VALUES ('201', 'Michael', 'Hartstein', 'MHARTSTE', '515.123.5555', 'MK_MAN', '13000.00', null, '100', '20', '2016-03-03 00:00:00');
    INSERT INTO employees VALUES ('202', 'Pat', 'Fay', 'PFAY', '603.123.6666', 'MK_REP', '6000.00', null, '201', '20', '2016-03-03 00:00:00');
    INSERT INTO employees VALUES ('203', 'Susan', 'Mavris', 'SMAVRIS', '515.123.7777', 'HR_REP', '6500.00', null, '101', '40', '2016-03-03 00:00:00');
    INSERT INTO employees VALUES ('204', 'Hermann', 'Baer', 'HBAER', '515.123.8888', 'PR_REP', '10000.00', null, '101', '70', '2016-03-03 00:00:00');
    INSERT INTO employees VALUES ('205', 'Shelley', 'Higgins', 'SHIGGINS', '515.123.8080', 'AC_MGR', '12000.00', null, '101', '110', '2016-03-03 00:00:00');
    INSERT INTO employees VALUES ('206', 'William', 'Gietz', 'WGIETZ', '515.123.8181', 'AC_ACCOUNT', '8300.00', null, '205', '110', '2016-03-03 00:00:00');
  • 相关阅读:
    Cocos2d-x基础篇C++
    wamp+thinkphp环境配置
    bootstrap table笔记
    写后台SQL的一些心得
    mybatis代码自动生成
    Maven+SpringMVC+MyBatis 上传图片
    五毛的cocos2d-x学习笔记08-动画
    五毛的cocos2d-x学习笔记07-计时器、数据读写、文件读写
    五毛的cocos2d-x学习笔记06-处理用户交互
    学习python笔记 协程
  • 原文地址:https://www.cnblogs.com/hfl1996/p/13330257.html
Copyright © 2011-2022 走看看