zoukankan      html  css  js  c++  java
  • MySQL 分组统计查询 表连接(3)

    1 查询底薪超过公司平均底薪的员工信息?

    select
    e.empno,e.ename,e.sal
    from t_emp as e
    join (select avg(sal) as avg from t_emp) t on e.sal > t.avg

    2 统计人数 格式化时间

    select
    count(*) as count, max(a.sal),min(a.sal),
    avg(a.sal) , floor(avg(DATEDIFF(NOW(),a.hiredate)/365))
    from
    t_emp a
    join t_dept b on a.deptno = b.deptno
    where
    b. dname = 'RESEARCH'

    3 查询每种职业的最高工资,最低工资 平均工资 ...

    select
    e.job,
    max(e.sal + IFNULL(e.comm,0)),
    min(e.sal + IFNULL(e.comm,0)),
    avg(e.sal + IFNULL(e.comm,0)),
    max(s.grade),
    min(s.grade)
    from t_emp e join t_salgrade s
    on e.sal + IFNULL(e.comm,0) BETWEEN s.losal and s.hisal
    GROUP BY e.job

    4 查询每个底薪超过部门平均底薪的员工

    select e.empno,
    e.ename,
    e.sal

    from t_emp e join
    (select deptno,AVG(sal) avg from t_emp GROUP BY deptno) t
    on e.deptno = t.deptno
    where e.sal >= t.avg

     5 统计部门名称和人数

    select
    d.dname,count(e.deptno)
    from t_dept d
    left join t_emp e on d.deptno = e.deptno
    GROUP BY d.deptno HAVING d.dname is not null

    6  union 连接使用方法

    (select
    d.dname,count(e.deptno)
    from t_dept d
    left join t_emp e on d.deptno = e.deptno
    GROUP BY d.deptno)
    union
    (select
    d.dname,count(*)
    from t_dept d
    right join t_emp e on d.deptno = e.deptno
    GROUP BY d.deptno)

    7 查询上司名称 编号

    select
    e.empno,
    e.ename,
    d.dname,
    (e.sal+ IFNULL(comm,0)) AS sal,
    s.grade,
    FLOOR(DATEDIFF(NOW(),e.hiredate)/365) AS hire,
    t.empno m_empno,
    t.ename m_ename,
    t.dname m_dname
    from
    t_emp e
    left join t_dept d on d.deptno = e.deptno
    left join t_salgrade s on e.sal BETWEEN s.losal and s.hisal
    left join (select
    e.deptno,e.empno,e.ename,d.dname
    from t_emp e
    join t_dept d on d.deptno = e.deptno) as t on t.empno = e.mgr


  • 相关阅读:
    Java集合和数组的区别
    二分法查找
    功能模块划分的原则及方法
    CentOS 6.5 开机启动指定服务
    CentOS 6.5配置mysql
    CentOS 6.5安装Tcpreplay
    CentOS6.5 常用命令
    CentOS6.5 安装ntopng-1.2.0
    【转】CentOS安装PF_RING(虚拟机)
    CentOS查询 杀死进程
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/11316910.html
Copyright © 2011-2022 走看看