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

    一:聚合函数

    count(*),返回行的数目。数值为null的不计算在内,distinct 去重复。

    sum()计算列中所有值得总和。

    avg()计算列中所有值得平均值。

    max()计算列中所有值得总最大值。

    min()计算列中所有值得最小值。

    二:分组的语句 group by

    三:having 子句

    四:执行顺序

    select--from--where--group--order by--having

    五:组合查询

    1,添加到其他查询的查询语句叫子查询。如果子查询的返回单行时,就可以简单的插入到查询中。但是返回多行时,就需要用到in关键字,但是需要注意的的是in前后的数据类型要匹配。

    2,子查询中可以用到关键字all和any.如果表中有相同的数据,all代表所有的满足。any代表任意一个满足就可。

    3,用union。把两个查询结果连接到一起,但是列名变为一个,而且数据类型要匹配。

    /*
    Navicat MySQL Data Transfer
    
    Source Server         : wode
    Source Server Version : 50022
    Source Host           : localhost:3306
    Source Database       : j121
    
    Target Server Type    : MYSQL
    Target Server Version : 50022
    File Encoding         : 65001
    
    Date: 2016-05-09 10:33:00
    */
    
    SET FOREIGN_KEY_CHECKS=0;
    -- ----------------------------
    -- Table structure for `dept`
    -- ----------------------------
    DROP TABLE IF EXISTS `dept`;
    CREATE TABLE `dept` (
      `deptNo` int(11) NOT NULL,
      `dname` varchar(20) default NULL,
      `loc` varchar(40) default NULL,
      PRIMARY KEY  (`deptNo`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    -- ----------------------------
    -- Records of dept
    -- ----------------------------
    INSERT INTO dept VALUES ('10', 'accounting', 'new york');
    INSERT INTO dept VALUES ('20', 'research', 'dallas');
    INSERT INTO dept VALUES ('30', 'sales', 'chicago');
    INSERT INTO dept VALUES ('40', 'operations', 'boston');
    
    -- ----------------------------
    -- Table structure for `emp`
    -- ----------------------------
    DROP TABLE IF EXISTS `emp`;
    CREATE TABLE `emp` (
      `id` int(11) NOT NULL auto_increment,
      `empno` int(11) default NULL,
      `eName` varchar(20) default NULL,
      `job` varchar(10) default NULL,
      `mgr` int(11) default NULL,
      `hireDate` date default NULL,
      `sal` decimal(7,2) default NULL,
      `comm` decimal(7,2) default NULL,
      `deptNo` int(11) default NULL,
      PRIMARY KEY  (`id`),
      KEY `FK_emp_deptNo` (`deptNo`),
      CONSTRAINT `FK_emp_deptNo` FOREIGN KEY (`deptNo`) REFERENCES `dept` (`deptNo`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    -- ----------------------------
    -- Records of emp
    -- ----------------------------
    INSERT INTO emp VALUES ('1', '7369', 'smith', 'clerk', '7902', '1980-12-17', '800.00', null, '20');
    INSERT INTO emp VALUES ('2', '7499', 'allen', 'salesman', '7698', '1981-02-20', '1600.00', '300.00', '30');
    INSERT INTO emp VALUES ('3', '7521', 'ward', 'salesman', '7698', '1981-02-22', '1250.00', '500.00', '30');
    INSERT INTO emp VALUES ('4', '7566', 'jones', 'manager', '7839', '1981-04-02', '2975.00', null, '20');
    INSERT INTO emp VALUES ('5', '7654', 'martin', 'salesman', '7698', '1981-09-28', '1250.00', '1400.00', '30');
    INSERT INTO emp VALUES ('6', '7698', 'blake', 'manager', '7839', '1981-05-01', '2850.00', null, '30');
    INSERT INTO emp VALUES ('7', '7782', 'clark', 'manager', '7839', '1981-06-09', '2450.00', null, '10');
    INSERT INTO emp VALUES ('8', '7788', 'scott', 'analyst', '7566', '1987-07-31', '3000.00', null, '20');
    INSERT INTO emp VALUES ('9', '7839', 'king', 'president', null, '1981-11-17', '5000.00', null, '10');
    INSERT INTO emp VALUES ('10', '7844', 'turner', 'salesman', '7698', '1981-09-08', '1500.00', '0.00', '30');
    INSERT INTO emp VALUES ('11', '7876', 'adams', 'clerk', '7788', '1987-07-13', '1100.00', null, '20');
    INSERT INTO emp VALUES ('12', '7900', 'james', 'clerk', '7698', '1981-12-03', '950.00', null, '30');
    INSERT INTO emp VALUES ('13', '7902', 'ford', 'analyst', '7566', '1981-12-03', '3000.00', null, '20');
    INSERT INTO emp VALUES ('14', '7934', 'miller', 'clerk', '7782', '1982-02-23', '1300.00', null, '10');


    上面的代码会创建emp表和dept表

    六:基本操作:

    1:emp表中查询公司总共有几个部门 注意,会查询出来大量重复的,使用函数distinct select distinct job from scott.emp;

    2:查询公司工资在1000-3000之间的人有哪些 使用函数between ...and.. select * from scott.emp where sal between 3000 and 5000;

    3:查询公司没有奖金的人 使用null 和“” 不一样 select * from scott.emp where comm is null;

    4:查询公司员工职位是'manager','clerk' 的人 select * from scott.emp where lower(job) in('manager','clerk'); 查询不是这两个职位的人 select * from scott.emp where upper(job) not in('MANAGER','CLERK');

    5:查询工资最高的人  查询每个部门工资最高的人?--分组查询 select  deptno,max(sal) from scott.emp group by deptno ;  完整版  29:-每个部门的最高薪水是多少  select * from scott.emp where (deptno,sal) in (select deptno,max(sal) from scott.emp group by deptno);

    7:-查询比20部门总人数多的部门 select deptno from scott.emp group by deptno having count(*) >(select count(*) from scott.emp where deptno=20);

    13:薪水由高到低排序( 降序排列 ) select empno,ename,sal from scott.emp order by sal desc; 14-按入职时间排序 , 入职时间越早排在前面 select hiredate from scott.emp order by hiredate; 15按部门排序 , 同一部门按薪水由高到低排序 select deptno,sal from scott.emp order by deptno,sal desc; 16计算员工的薪水总和是多少?sum() select sum(sal)+sum(comm) from scott.emp; 17计算最早和最晚的员工入职时间  select min(hiredate),max(hiredate) from scott.emp;

    (查最早和最晚入职的人的信息) select * from emp where hireDate in (select max(hireDate) from emp) UNION select * from emp where hireDate in (select min(hireDate) from emp)

    18按部门计算每个部门的最高和最低薪水分别是多少 select deptno,max(sal),min(sal) from scott.emp group by deptno order by deptno;

    19计算每个部门的 薪水总和 和 平均薪水? select deptno,sum(sal),avg(sal) from scott.emp group by deptno;

    20按职位分组 , 每个职位的最高、最低薪水和人数? select job,max(sal),min(sal),count(*) from scott.emp group by job;

    21平均薪水大于5000元的部门数据` select deptno,avg(sal) from scott.emp group by deptno having avg(sal)>2000; 22薪水总和大于20000元的部门数据 select deptno,sum(sal) from scott.emp group by deptno having sum(sal)>10000; 23:哪些职位的人数超过2个人 select job,count(*) from scott.emp group by job having count(*)>2;

    24:查询最高薪水的是谁?-查询最低薪水的员工 select ename from scott.emp where sal=(select max(sal) from scott.emp);

    25:谁的工资比smith高 select ename from scott.emp where sal>(select sal from scott.emp where lower(ename)='smith');

    26销售部门有哪些职位存在 select job from scott.emp where deptno=(select deptno from scott.dept where lower(dname)='sales');

    --思考: -- 谁的工资比smith高这道题目中如果存在两个角simth的人会怎么样?     --all:满足全部     --any:任意一个     insert into scott.emp(empno,ename,sal) values(9527,'smith',888);       select ename from scott.emp where sal>all(select sal from scott.emp where lower(ename)='smith');

    27-查询谁和smith是同一个部门的员工 select empno,ename,deptno from scott.emp where deptno=(select deptno from scott.emp where lower(ename)='smith');

    28查询谁是员JAMES的下属 select empno,ename from scott.emp where mgr in(select mgr from scott.emp where lower(ename)='james');

    30哪个部门的人数比部门20的人数多 select deptno,count(*) from scott.emp group by deptno having count(*)> (select count(*) from scott.emp where deptno=20);

    31:哪些员工的薪水比公司的平均薪水低? select empno,ename from scott.emp where sal<(select avg(sal) from scott.emp);

  • 相关阅读:
    Python3 MySQL 数据库连接
    python3中线程池
    python中paramiko模块和mysql数据库的操作
    linux定制的补充
    linux 定制
    利用BIND搭建自己的私有根及授权域
    Python3下的paramiko模块
    mysql在linux下的安装与优化
    Linux下Nagios的安装与配置
    CentOS 6.7中安装python3.5
  • 原文地址:https://www.cnblogs.com/Kevin-Ma/p/5479756.html
Copyright © 2011-2022 走看看