【MySQL】MySQL复合查询--多表查询自连接子查询 - 副本

文章目录
  • 1.基本查询回顾
  • 2.多表查询
  • 3.自连接
  • 4.子查询
    • 4.1单行子查询
    • 4.2多行子查询
    • 4.3多列子查询
    • 4.4在from子句中使用子查询
    • 4.5合并查询
      • 4.5.1 union
      • 4.5.2 union all

1.基本查询回顾

表的内容如下:

mysql> select * from emp;
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno  | ename  | job       | mgr  | hiredate            | sal     | comm    | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 007369 | SMITH  | CLERK     | 7902 | 1980-12-17 00:00:00 |  800.00 |    NULL |     20 |
| 007499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 |  300.00 |     30 |
| 007521 | WARD   | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 |  500.00 |     30 |
| 007566 | JONES  | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 |    NULL |     20 |
| 007654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |
| 007698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 |    NULL |     30 |
| 007782 | CLARK  | MANAGER   | 7839 | 1981-06-09 00:00:00 | 2450.00 |    NULL |     10 |
| 007788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 |    NULL |     20 |
| 007839 | KING   | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |    NULL |     10 |
| 007844 | TURNER | SALESMAN  | 7698 | 1981-09-08 00:00:00 | 1500.00 |    0.00 |     30 |
| 007876 | ADAMS  | CLERK     | 7788 | 1987-05-23 00:00:00 | 1100.00 |    NULL |     20 |
| 007900 | JAMES  | CLERK     | 7698 | 1981-12-03 00:00:00 |  950.00 |    NULL |     30 |
| 007902 | FORD   | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 |    NULL |     20 |
| 007934 | MILLER | CLERK     | 7782 | 1982-01-23 00:00:00 | 1300.00 |    NULL |     10 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.00 sec)

mysql> select * from dept;
+--------+------------+----------+
| deptno | dname      | loc      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.00 sec)

mysql> select * from salgrade;
+-------+-------+-------+
| grade | losal | hisal |
+-------+-------+-------+
|     1 |   700 |  1200 |
|     2 |  1201 |  1400 |
|     3 |  1401 |  2000 |
|     4 |  2001 |  3000 |
|     5 |  3001 |  9999 |
+-------+-------+-------+
5 rows in set (0.00 sec)
  • 查询工资高于500或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J

    // 使用模糊查询
    select * from emp where (sal>500 or job=‘MANAGER’) and ename like ‘J%’;
    // 使用函数
    select * from emp where (sal>500 or job=‘MANAGER’) and substring(ename,1,1)=‘J’;
    mysql> select * from emp where (sal>500 or job=‘MANAGER’) and ename like ‘J%’;
    ±-------±------±--------±-----±--------------------±--------±-----±-------+
    | empno | ename | job | mgr | hiredate | sal | comm | deptno |
    ±-------±------±--------±-----±--------------------±--------±-----±-------+
    | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
    | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
    ±-------±------±--------±-----±--------------------±--------±-----±-------+
    2 rows in set (0.00 sec)

    mysql> select * from emp where (sal>500 or job=‘MANAGER’) and substring(ename,1,1)=‘J’;
    ±-------±------±--------±-----±--------------------±--------±-----±-------+
    | empno | ename | job | mgr | hiredate | sal | comm | deptno |
    ±-------±------±--------±-----±--------------------±--------±-----±-------+
    | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
    | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
    ±-------±------±--------±-----±--------------------±--------±-----±-------+
    2 rows in set (0.00 sec)

在这里插入图片描述

  • 按照部门号升序而雇员的工资降序排序

    select * from emp order by deptno asc, sal desc;
    mysql> select * from emp order by deptno asc,sal desc;
    ±-------±-------±----------±-----±--------------------±--------±--------±-------+
    | empno | ename | job | mgr | hiredate | sal | comm | deptno |
    ±-------±-------±----------±-----±--------------------±--------±--------±-------+
    | 007839 | KING | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL | 10 |
    | 007782 | CLACK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL | 10 |
    | 007934 | MILLER | CLERK | 7782 | 1982-01-23 00:00:00 | 1300.00 | NULL | 10 |
    | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 |
    | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
    | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
    | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 |
    | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 |
    | 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL | 30 |
    | 007499 | ALLEN | SALESMAN | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 | 30 |
    | 007844 | TURNER | SALESMAN | 7698 | 1981-09-08 00:00:00 | 1500.00 | 0.00 | 30 |
    | 007521 | WARD | SALESMAN | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00 | 30 |
    | 007654 | MARTIN | SALESMAN | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 | 30 |
    | 007900 | JAMES | CLERK | 7698 | 1981-12-03 00:00:00 | 950.00 | NULL | 30 |
    ±-------±-------±----------±-----±--------------------±--------±--------±-------+

在这里插入图片描述

  • 使用年薪进行降序排序

    年薪等于工资*12+奖金

    需要对奖金进行判断,如果奖金为null,则奖金为0

    select ename, sal*12+ifnull(comm,0) as ‘年薪’ from emp order by 年薪 desc;

    mysql> select ename,sal*12+ifnull(comm,0) as ‘年薪’ from emp order by 年薪 desc;
    ±-------±---------+
    | ename | 年薪 |
    ±-------±---------+
    | SMITH | 9600.00 |
    | ALLEN | 19500.00 |
    | WARD | 15500.00 |
    | JONES | 35700.00 |
    | MARTIN | 16400.00 |
    | BLAKE | 34200.00 |
    | TEST | 29400.00 |
    | SCOTT | 36000.00 |
    | KING | 60000.00 |
    | TURNER | 18000.00 |
    | ADAMS | 13200.00 |
    | JAMES | 11400.00 |
    | FORD | 36000.00 |
    | MILLER | 15600.00 |
    ±-------±---------+
    14 rows in set (0.00 sec)

在这里插入图片描述

  • 显示工资最高的员工的名字和工作岗位

    这里使用分组查询即可,先查出最高的工资,然后查询工资等于最高工资的员工的姓名和工作岗位

    select ename,job from emp where sal = (select max(sal) from emp);

    mysql> select ename,job from emp where sal = (select max(sal) from emp);
    ±------±----------+
    | ename | job |
    ±------±----------+
    | KING | PRESIDENT |
    ±------±----------+
    1 row in set (0.00 sec)

在这里插入图片描述

  • 显示工资高于平均工资的员工信息

    这里使用分组查询即可

    select ename,sal from emp where sal > (select avg(sal) from emp);

    mysql> select ename,sal from emp where sal > (select avg(sal) from emp);
    ±------±--------+
    | ename | sal |
    ±------±--------+
    | JONES | 2975.00 |
    | BLAKE | 2850.00 |
    | TEST | 2450.00 |
    | SCOTT | 3000.00 |
    | KING | 5000.00 |
    | FORD | 3000.00 |
    ±------±--------+
    6 rows in set (0.00 sec)

在这里插入图片描述

  • 显示每个部门的平均工资和最高工资

    select deptno,avg(sal),max(sal) from emp group by deptno;

    mysql> select deptno,avg(sal),max(sal) from emp group by deptno;
    ±-------±------------±---------+
    | deptno | avg(sal) | max(sal) |
    ±-------±------------±---------+
    | 10 | 2425.000000 | 5000.00 |
    | 20 | 2175.000000 | 3000.00 |
    | 30 | 1690.000000 | 2850.00 |
    ±-------±------------±---------+
    3 rows in set (0.00 sec)

在这里插入图片描述

  • 显示平均工资低于2000的部门号和它的平均工资

    select deptno,avg(sal) as avg_sal from emp group by deptno having avg_sal < 2000;

    mysql> select deptno,avg(sal) as avg_sal from emp group by deptno having avg_sal < 2000;
    ±-------±------------+
    | deptno | avg_sal |
    ±-------±------------+
    | 30 | 1690.000000 |
    ±-------±------------+
    1 row in set (0.00 sec)

在这里插入图片描述

  • 显示每种岗位的雇员总数,平均工资

    select job,count(*), avg(sal) from emp group by job;

    mysql> select job,count(), avg(sal) from emp group by job;
    ±----------±---------±------------+
    | job | count(
    ) | avg(sal) |
    ±----------±---------±------------+
    | ANALYST | 2 | 3000.000000 |
    | CLERK | 4 | 1037.500000 |
    | MANAGER | 3 | 2758.333333 |
    | PRESIDENT | 1 | 5000.000000 |
    | SALESMAN | 4 | 1400.000000 |
    ±----------±---------±------------+
    5 rows in set (0.00 sec)

在这里插入图片描述

2.多表查询

实际开发中往往数据来自不同的表,所以需要多表查询。本节我们用一个简单的公司管理系统,有三张

表emp,dept,salgrade来演示如何进行多表查询。

案例:

显示雇员名、雇员工资以及所在部门的名字因为上面的数据来自emp和dept表,因此要联合查询

在这里插入图片描述

其实我们只要emp表中的deptno = dept表中的deptno字段的记录

select ename,sal,dname from emp,dept where emp.deptno=dept.deptno;
mysql> select ename,sal,dname from emp,dept where emp.deptno=dept.deptno;
+--------+---------+------------+
| ename  | sal     | dname      |
+--------+---------+------------+
| SMITH  |  800.00 | RESEARCH   |
| ALLEN  | 1600.00 | SALES      |
| WARD   | 1250.00 | SALES      |
| JONES  | 2975.00 | RESEARCH   |
| MARTIN | 1250.00 | SALES      |
| BLAKE  | 2850.00 | SALES      |
| CLACK  | 2450.00 | ACCOUNTING |
| SCOTT  | 3000.00 | RESEARCH   |
| KING   | 5000.00 | ACCOUNTING |
| TURNER | 1500.00 | SALES      |
| ADAMS  | 1100.00 | RESEARCH   |
| JAMES  |  950.00 | SALES      |
| FORD   | 3000.00 | RESEARCH   |
| MILLER | 1300.00 | ACCOUNTING |
+--------+---------+------------+
14 rows in set (0.00 sec)

在这里插入图片描述

  • 显示部门号为10的部门名,员工名和工资

    mysql> select dname,ename,sal from emp,dept where emp.deptno=dept.deptno and dept.deptno=10;
    ±-----------±-------±--------+
    | dname | ename | sal |
    ±-----------±-------±--------+
    | ACCOUNTING | CLACK | 2450.00 |
    | ACCOUNTING | KING | 5000.00 |
    | ACCOUNTING | MILLER | 1300.00 |
    ±-----------±-------±--------+
    3 rows in set (0.00 sec)

在这里插入图片描述

  • 显示各个员工的姓名,工资,及工资级别

    mysql> select ename,sal,grade from emp,salgrade where sal between losal and hisal;
    mysql> select ename,sal,grade from emp,salgrade where sal between losal and hisal;
    ±-------±--------±------+
    | ename | sal | grade |
    ±-------±--------±------+
    | SMITH | 800.00 | 1 |
    | ALLEN | 1600.00 | 3 |
    | WARD | 1250.00 | 2 |
    | JONES | 2975.00 | 4 |
    | MARTIN | 1250.00 | 2 |
    | BLAKE | 2850.00 | 4 |
    | CLACK | 2450.00 | 4 |
    | SCOTT | 3000.00 | 4 |
    | KING | 5000.00 | 5 |
    | TURNER | 1500.00 | 3 |
    | ADAMS | 1100.00 | 1 |
    | JAMES | 950.00 | 1 |
    | FORD | 3000.00 | 4 |
    | MILLER | 1300.00 | 2 |
    ±-------±--------±------+
    14 rows in set (0.00 sec)

在这里插入图片描述

3.自连接

自连接是指在同一张表连接查询

案例:

显示员工FORD的上级领导的编号和姓名(mgr是员工领导的编号–empno)

使用的子查询

select ename,empno from emp where empno=(select mgr from emp where ename='FORD');

使用多表查询(自查询)

select e2.ename,e2.empno from emp e1,emp e2 where e1.ename='FORD' and e1.mgr=e2.empno;
mysql> select e1.ename,e2.empno from emp e1,emp e2 where e1.ename='FORD' and e1.mgr=e2.empno;
+-------+--------+
| ename | empno  |
+-------+--------+
| FORD  | 007566 |
+-------+--------+
1 row in set (0.00 sec)

在这里插入图片描述

4.子查询

子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询

4.1单行子查询

返回一行记录的子查询

  • 显示SMITH同一部门的员工

    select * from emp where deptno=(select deptno from emp where ename=‘SMITH’);
    mysql> select * from emp where deptno=(select deptno from emp where ename=‘SMITH’);
    ±-------±------±--------±-----±--------------------±--------±-----±-------+
    | empno | ename | job | mgr | hiredate | sal | comm | deptno |
    ±-------±------±--------±-----±--------------------±--------±-----±-------+
    | 007369 | SMITH | CLERK | 7902 | 1980-12-17 00:00:00 | 800.00 | NULL | 20 |
    | 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL | 20 |
    | 007788 | SCOTT | ANALYST | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL | 20 |
    | 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL | 20 |
    | 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
    ±-------±------±--------±-----±--------------------±--------±-----±-------+
    5 rows in set (0.00 sec)

在这里插入图片描述

4.2多行子查询

返回多行记录的子查询

  • in关键字;查询和10号部门的工作岗位相同的雇员的名字,岗位,工资,部门号,但是不包含10自己的

    select ename,job,sal,deptno from emp where job in(select job from emp where deptno=10) and deptno<>10;
    mysql> select ename,job,sal,deptno from emp where job in(select job from emp where deptno=10) and deptno<>10;
    +-------+---------+---------+--------+
    | ename | job     | sal     | deptno |
    +-------+---------+---------+--------+
    | JONES | MANAGER | 2975.00 |     20 |
    | BLAKE | MANAGER | 2850.00 |     30 |
    | SMITH | CLERK   |  800.00 |     20 |
    | ADAMS | CLERK   | 1100.00 |     20 |
    | JAMES | CLERK   |  950.00 |     30 |
    +-------+---------+---------+--------+
    5 rows in set (0.00 sec)
    
  • all关键字;显示工资比部门30的所有员工的工资高的员工的姓名、工资和部门号

    // 使用聚合函数
    select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30);
    mysql> select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30);
    +-------+---------+--------+
    | ename | sal     | deptno |
    +-------+---------+--------+
    | JONES | 2975.00 |     20 |
    | SCOTT | 3000.00 |     20 |
    | KING  | 5000.00 |     10 |
    | FORD  | 3000.00 |     20 |
    +-------+---------+--------+
    4 rows in set (0.01 sec)
    
    // 使用all关键子
    select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=30);
    mysql> select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=30);
    +-------+---------+--------+
    | ename | sal     | deptno |
    +-------+---------+--------+
    | JONES | 2975.00 |     20 |
    | SCOTT | 3000.00 |     20 |
    | KING  | 5000.00 |     10 |
    | FORD  | 3000.00 |     20 |
    +-------+---------+--------+
    4 rows in set (0.00 sec)
    
  • any关键字;显示工资比部门30的任意员工的工资高的员工的姓名、工资和部门号(包含自己部门的员工)

    // 使用聚合函数
    mysql> select ename,sal,deptno from emp where sal > (select min(sal) from emp where deptno=30) and deptno<>30;
    ±-------±--------±-------+
    | ename | sal | deptno |
    ±-------±--------±-------+
    | JONES | 2975.00 | 20 |
    | CLACK | 2450.00 | 10 |
    | SCOTT | 3000.00 | 20 |
    | KING | 5000.00 | 10 |
    | ADAMS | 1100.00 | 20 |
    | FORD | 3000.00 | 20 |
    | MILLER | 1300.00 | 10 |
    ±-------±--------±-------+
    7 rows in set (0.00 sec)
    // 使用any关键字
    mysql> select ename,sal,deptno from emp where sal > any(select sal from emp where deptno=30) and deptno<>30;
    ±-------±--------±-------+
    | ename | sal | deptno |
    ±-------±--------±-------+
    | JONES | 2975.00 | 20 |
    | CLACK | 2450.00 | 10 |
    | SCOTT | 3000.00 | 20 |
    | KING | 5000.00 | 10 |
    | ADAMS | 1100.00 | 20 |
    | FORD | 3000.00 | 20 |
    | MILLER | 1300.00 | 10 |
    ±-------±--------±-------+
    7 rows in set (0.00 sec)

4.3多列子查询

单行子查询是指子查询只返回单列,单行数据;多行子查询是指返回单列多行数据,都是针对单列而言

的,而多列子查询则是指查询返回多个列数据的子查询语句

案例:查询和SMITH的部门和岗位完全相同的所有雇员,不含SMITH本人

mysql> select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH') and ename<>'SMITH';
mysql> select * from emp where (deptno,job)in(select deptno,job from emp where ename='SMITH') and ename<>'SMITH';
+--------+-------+-------+------+---------------------+---------+------+--------+
| empno  | ename | job   | mgr  | hiredate            | sal     | comm | deptno |
+--------+-------+-------+------+---------------------+---------+------+--------+
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL |     20 |
+--------+-------+-------+------+---------------------+---------+------+--------+
1 row in set (0.00 sec)

4.4在from子句中使用子查询

子查询语句出现在from子句中。这里要用到数据查询的技巧,把一个子查询当做一个临时表使用。

案例:

显示每个高于自己部门平均工资的员工的姓名、部门、工资、平均工资

答案:

select t1.ename,t1.deptno,t1.sal,t2.myavg from emp t1,(select deptno,avg(sal) myavg from emp group by deptno) t2 where t1.deptno=t2.deptno and t1.ssal > t2.myavg;

步骤:

// 1.根据部门号分组得到每组的平均工资
mysql> select avg(sal) from emp group by deptno;
+-------------+
| avg(sal)    |
+-------------+
| 2916.666667 |
| 2175.000000 |
| 1566.666667 |
+-------------+
3 rows in set (0.00 sec)

// 2.根据部门号分组得到每组的平均工资和部门号
mysql> select deptno,avg(sal) from emp group by deptno;
+--------+-------------+
| deptno | avg(sal)    |
+--------+-------------+
|     10 | 2916.666667 |
|     20 | 2175.000000 |
|     30 | 1566.666667 |
+--------+-------------+
3 rows in set (0.00 sec)

// 3.将上面得到的结果与emp表做笛卡尔积
mysql> select * from emp t1,(select deptno,avg(sal) myavg from emp group by deptno) t2 where t1.deptno=t2.deptno;
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+-------------+
| empno  | ename  | job       | mgr  | hiredate            | sal     | comm    | deptno | deptno | myavg       |
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+-------------+
| 007369 | SMITH  | CLERK     | 7902 | 1980-12-17 00:00:00 |  800.00 |    NULL |     20 |     20 | 2175.000000 |
| 007499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 |  300.00 |     30 |     30 | 1566.666667 |
| 007521 | WARD   | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 |  500.00 |     30 |     30 | 1566.666667 |
| 007566 | JONES  | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 |    NULL |     20 |     20 | 2175.000000 |
| 007654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |     30 | 1566.666667 |
| 007698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 |    NULL |     30 |     30 | 1566.666667 |
| 007782 | CLACK  | MANAGER   | 7839 | 1981-06-09 00:00:00 | 2450.00 |    NULL |     10 |     10 | 2916.666667 |
| 007788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 |    NULL |     20 |     20 | 2175.000000 |
| 007839 | KING   | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |    NULL |     10 |     10 | 2916.666667 |
| 007844 | TURNER | SALESMAN  | 7698 | 1981-09-08 00:00:00 | 1500.00 |    0.00 |     30 |     30 | 1566.666667 |
| 007876 | ADAMS  | CLERK     | 7788 | 1987-05-23 00:00:00 | 1100.00 |    NULL |     20 |     20 | 2175.000000 |
| 007900 | JAMES  | CLERK     | 7698 | 1981-12-03 00:00:00 |  950.00 |    NULL |     30 |     30 | 1566.666667 |
| 007902 | FORD   | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 |    NULL |     20 |     20 | 2175.000000 |
| 007934 | MILLER | CLERK     | 7782 | 1982-01-23 00:00:00 | 1300.00 |    NULL |     10 |     10 | 2916.666667 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+-------------+
14 rows in set (0.00 sec)

// 5.增加筛选条件 :工资大于平均工资
mysql> select * from emp t1,(select deptno,avg(sal) myavg from emp group by deptno) t2 where t1.deptno=t2.deptno and t1.sal > t2.myavg;
+--------+-------+-----------+------+---------------------+---------+--------+--------+--------+-------------+
| empno  | ename | job       | mgr  | hiredate            | sal     | comm   | deptno | deptno | myavg       |
+--------+-------+-----------+------+---------------------+---------+--------+--------+--------+-------------+
| 007499 | ALLEN | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00 |     30 |     30 | 1566.666667 |
| 007566 | JONES | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 |   NULL |     20 |     20 | 2175.000000 |
| 007698 | BLAKE | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 |   NULL |     30 |     30 | 1566.666667 |
| 007788 | SCOTT | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 |   NULL |     20 |     20 | 2175.000000 |
| 007839 | KING  | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 |   NULL |     10 |     10 | 2916.666667 |
| 007902 | FORD  | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 |   NULL |     20 |     20 | 2175.000000 |
+--------+-------+-----------+------+---------------------+---------+--------+--------+--------+-------------+
6 rows in set (0.00 sec)

// 5.根据题目要求得到结果
mysql> select t1.ename,t1.deptno,t1.sal,t2.myavg from emp t1,(select deptno,avg(sal) myavg from emp group by deptno) t2 where t1.deptno=t2.deptno and t1.ssal > t2.myavg;
+-------+--------+---------+-------------+
| ename | deptno | sal     | myavg       |
+-------+--------+---------+-------------+
| ALLEN |     30 | 1600.00 | 1566.666667 |
| JONES |     20 | 2975.00 | 2175.000000 |
| BLAKE |     30 | 2850.00 | 1566.666667 |
| SCOTT |     20 | 3000.00 | 2175.000000 |
| KING  |     10 | 5000.00 | 2916.666667 |
| FORD  |     20 | 3000.00 | 2175.000000 |
+-------+--------+---------+-------------+
6 rows in set (0.00 sec)

查找每个部门工资最高的人的姓名、工资、部门、最高工资

答案:

select t1.ename,t1.sal,t1.deptno,t2.mymax from emp t1,(select deptno, max(sal) mymax from emp group by deptno) t2 where t1.deptno=t2.deptno and t1..sal=t2.mymax;

步骤:

// 1.得到分组之后的部门号和最高工资
mysql> select deptno, max(sal) from emp group by deptno;
+--------+----------+
| deptno | max(sal) |
+--------+----------+
|     10 |  5000.00 |
|     20 |  3000.00 |
|     30 |  2850.00 |
+--------+----------+
3 rows in set (0.01 sec)

// 2.与emp表进行笛卡尔积并进行t1.sal=t2.mymax的筛选(工资等于最高工资)
mysql> select * from emp t1,(select deptno, max(sal) mymax from emp group by deptno) t2 where t1.deptno=t2.deptno and t1.sal=t2.mymax;
+--------+-------+-----------+------+---------------------+---------+------+--------+--------+---------+
| empno  | ename | job       | mgr  | hiredate            | sal     | comm | deptno | deptno | mymax   |
+--------+-------+-----------+------+---------------------+---------+------+--------+--------+---------+
| 007698 | BLAKE | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL |     30 |     30 | 2850.00 |
| 007788 | SCOTT | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL |     20 |     20 | 3000.00 |
| 007839 | KING  | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL |     10 |     10 | 5000.00 |
| 007902 | FORD  | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL |     20 |     20 | 3000.00 |
+--------+-------+-----------+------+---------------------+---------+------+--------+--------+---------+
4 rows in set (0.00 sec)

// 3.根据题目要求选择需要筛选的内容
mysql> select t1.ename,t1.sal,t1.deptno,t2.mymax from emp t1,(select deptno, max(sal) mymax from emp group by deptno) t2 where t1.deptno=t2.deptno and t1..sal=t2.mymax;
+-------+---------+--------+---------+
| ename | sal     | deptno | mymax   |
+-------+---------+--------+---------+
| BLAKE | 2850.00 |     30 | 2850.00 |
| SCOTT | 3000.00 |     20 | 3000.00 |
| KING  | 5000.00 |     10 | 5000.00 |
| FORD  | 3000.00 |     20 | 3000.00 |
+-------+---------+--------+---------+
4 rows in set (0.00 sec

显示每个部门的信息(部门名,编号,地址)和人员数量

答案:

select t1.deptno,t1.dname,t1.loc,t2.num from dept t1,(select deptno,count(*) num from emp group by deptno) t2 where t1.deptno=t2.deptno;

步骤:

// 1.分组得到每一组的人数
mysql> select deptno,count(*) num from emp group by deptno;
+--------+-----+
| deptno | num |
+--------+-----+
|     10 |   3 |
|     20 |   5 |
|     30 |   6 |
+--------+-----+
3 rows in set (0.00 sec)

// 2.和部门表进行笛卡尔积,然后进行条件筛选
mysql> select * from dept t1,(select deptno,count(*) num from emp group by deptno) t2 where t1.deptno=t2.deptno;
+--------+------------+----------+--------+-----+
| deptno | dname      | loc      | deptno | num |
+--------+------------+----------+--------+-----+
|     10 | ACCOUNTING | NEW YORK |     10 |   3 |
|     20 | RESEARCH   | DALLAS   |     20 |   5 |
|     30 | SALES      | CHICAGO  |     30 |   6 |
+--------+------------+----------+--------+-----+
3 rows in set (0.01 sec)
mysql> select t1.deptno,t1.dname,t1.loc,t2.num from dept t1,(select deptno,count(*) num from emp group by deptno) t2 where t1.deptno=t2.deptno;
+--------+------------+----------+-----+
| deptno | dname      | loc      | num |
+--------+------------+----------+-----+
|     10 | ACCOUNTING | NEW YORK |   3 |
|     20 | RESEARCH   | DALLAS   |   5 |
|     30 | SALES      | CHICAGO  |   6 |
+--------+------------+----------+-----+
3 rows in set (0.00 sec)

暴力解法:

mysql> select dept.dname,dept.deptno,dept.loc,count(*) from emp,dept where emp.deptno=dept.deptno group by dept.deptno,dept.dname,dept.loc;
+------------+--------+----------+----------+
| dname      | deptno | loc      | count(*) |
+------------+--------+----------+----------+
| ACCOUNTING |     10 | NEW YORK |        3 |
| RESEARCH   |     20 | DALLAS   |        5 |
| SALES      |     30 | CHICAGO  |        6 |
+------------+--------+----------+----------+
3 rows in set (0.01 sec)

总结:

解决多表问题的本质:想办法将多表转化为单表,所以mysql中,所有select的问题全部都可以转化成单表问题

4.5合并查询

在实际应用中,为了合并多个select的执行结果,可以使用集合操作符 union,union all

4.5.1 union

该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。

案例:将工资大于2500或职位是MANAGER的人找出

// 1.查出工资大于2500的
mysql> select * from emp where sal>2500;
+--------+-------+-----------+------+---------------------+---------+------+--------+
| empno  | ename | job       | mgr  | hiredate            | sal     | comm | deptno |
+--------+-------+-----------+------+---------------------+---------+------+--------+
| 007566 | JONES | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |     20 |
| 007698 | BLAKE | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL |     30 |
| 007788 | SCOTT | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL |     20 |
| 007839 | KING  | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL |     10 |
| 007902 | FORD  | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL |     20 |
+--------+-------+-----------+------+---------------------+---------+------+--------+
5 rows in set (0.00 sec)

// 2.查出job=MANAGER的
mysql> select * from emp where job='MANAGER';
+--------+-------+---------+------+---------------------+---------+------+--------+
| empno  | ename | job     | mgr  | hiredate            | sal     | comm | deptno |
+--------+-------+---------+------+---------------------+---------+------+--------+
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |     20 |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL |     30 |
| 007782 | CLACK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL |     10 |
+--------+-------+---------+------+---------------------+---------+------+--------+
3 rows in set (0.00 sec)

// 3.进行合并
mysql> select * from emp where sal>2500 union select * from emp where job='MANAGER';
+-------+-------+-----------+------+---------------------+---------+------+--------+
| empno | ename | job       | mgr  | hiredate            | sal     | comm | deptno |
+-------+-------+-----------+------+---------------------+---------+------+--------+
|  7566 | JONES | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |     20 |
|  7698 | BLAKE | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL |     30 |
|  7788 | SCOTT | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL |     20 |
|  7839 | KING  | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL |     10 |
|  7902 | FORD  | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL |     20 |
|  7782 | CLACK | MANAGER   | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL |     10 |
+-------+-------+-----------+------+---------------------+---------+------+--------+
6 rows in set (0.00 sec)
4.5.2 union all

操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。

案例:将工资大于25000或职位是MANAGER的人找出来

// 1.查出工资大于2500的
mysql> select * from emp where sal>2500;
+--------+-------+-----------+------+---------------------+---------+------+--------+
| empno  | ename | job       | mgr  | hiredate            | sal     | comm | deptno |
+--------+-------+-----------+------+---------------------+---------+------+--------+
| 007566 | JONES | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |     20 |
| 007698 | BLAKE | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL |     30 |
| 007788 | SCOTT | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL |     20 |
| 007839 | KING  | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL |     10 |
| 007902 | FORD  | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL |     20 |
+--------+-------+-----------+------+---------------------+---------+------+--------+
5 rows in set (0.00 sec)

// 2.查出job=MANAGER的
mysql> select * from emp where job='MANAGER';
+--------+-------+---------+------+---------------------+---------+------+--------+
| empno  | ename | job     | mgr  | hiredate            | sal     | comm | deptno |
+--------+-------+---------+------+---------------------+---------+------+--------+
| 007566 | JONES | MANAGER | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |     20 |
| 007698 | BLAKE | MANAGER | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL |     30 |
| 007782 | CLACK | MANAGER | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL |     10 |
+--------+-------+---------+------+---------------------+---------+------+--------+
3 rows in set (0.01 sec)

// 3.进行合并
mysql> select * from emp where sal>2500 union all select * from emp where job='MANAGER';
+-------+-------+-----------+------+---------------------+---------+------+--------+
| empno | ename | job       | mgr  | hiredate            | sal     | comm | deptno |
+-------+-------+-----------+------+---------------------+---------+------+--------+
|  7566 | JONES | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |     20 |
|  7698 | BLAKE | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL |     30 |
|  7788 | SCOTT | ANALYST   | 7566 | 1987-04-19 00:00:00 | 3000.00 | NULL |     20 |
|  7839 | KING  | PRESIDENT | NULL | 1981-11-17 00:00:00 | 5000.00 | NULL |     10 |
|  7902 | FORD  | ANALYST   | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL |     20 |
|  7566 | JONES | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL |     20 |
|  7698 | BLAKE | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL |     30 |
|  7782 | CLACK | MANAGER   | 7839 | 1981-06-09 00:00:00 | 2450.00 | NULL |     10 |
+-------+-------+-----------+------+---------------------+---------+------+--------+
8 rows in set (0.00 sec)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/415215.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

ubuntu安装新版本的CMake

来到cmake官网选择版本 我需要在嵌入式板子上的Ubuntu18安装使用 故我选择aarch64版本。 按F12进入检查模式得到下载链接。 在板子上运行以下命令&#xff0c;获取安装脚本 wget https://github.com/Kitware/CMake/releases/download/v3.28.3/cmake-3.28.3-linux-aarch64.s…

Django模板(四)

一、include标签 加载一个模板,并在当前上下文中进行渲染。这是一种在模板中 “包含” 其他模板的方式 简单的理解:在当前模板中引入另外一个模板内容 1.1、使用方法 模板名称可以是变量,也可以是单引号或双引号的硬编码(带引号)的字符串 {% include "foo/bar.ht…

接口自动化测试之HTTP协议详解

协议 简单理解&#xff0c;计算机与计算机之间的通讯语言就叫做协议&#xff0c;不同的计算机之间只有使用相同的协议才能通信。所以网络协议就是为计算机网络中进行数据交换而建立的规则&#xff0c;标准或约定的集合。 OSI模型 1978年国际化标准组织提出了“开放系统互联网…

BerDiff: Conditional Bernoulli Diffusion Modelfor Medical Image Segmentation

BerDiff:用于医学图像分割的条件伯努利扩散模型 摘要&#xff1a; 医学图像分割是一项具有挑战性的任务&#xff0c;具有固有的模糊性和高度的不确定性&#xff0c;这主要是由于肿瘤边界不明确和多个似是而非的注释等因素。分割口罩的准确性和多样性对于在临床实践中为放射科…

【UVM_Introduction Factory_2024.02.28】

Introduction 通用验证方法学UVM&#xff08;2014年1.2版本延续至今&#xff09; 作用&#xff1a; 降低验证工程复杂度&#xff0c;保证验证可靠性&#xff0c;提升验证效率 提供一套标准的类库&#xff0c;减轻环境构建的负担&#xff0c;更多的投入制定验证计划和创建测试场…

springboot228高校教师电子名片系统

高校教师电子名片系统的设计与实现 摘 要 传统信息的管理大部分依赖于管理人员的手工登记与管理&#xff0c;然而&#xff0c;随着近些年信息技术的迅猛发展&#xff0c;让许多比较老套的信息管理模式进行了更新迭代&#xff0c;名片信息因为其管理内容繁杂&#xff0c;管理数…

【数据结构】数组

第一章、为什么数组的下标一般从0开始编号 提到数组&#xff0c;读者肯定不陌生&#xff0c;甚至还会很自信地说&#xff0c;数组很简单。编程语言中一般会有数组这种数据类型。不过&#xff0c;它不仅是编程语言中的一种数据类型&#xff0c;还是基础的数据结构。尽管数组看起…

代码随想录算法训练营29期|day64 任务以及具体安排

第十章 单调栈part03 有了之前单调栈的铺垫&#xff0c;这道题目就不难了。 84.柱状图中最大的矩形class Solution {int largestRectangleArea(int[] heights) {Stack<Integer> st new Stack<Integer>();// 数组扩容&#xff0c;在头和尾各加入一个元素int [] ne…

半小时到秒级,京东零售定时任务优化怎么做的?

导言&#xff1a; 京东零售技术团队通过真实线上案例总结了针对海量数据批处理任务的一些通用优化方法&#xff0c;除了供大家借鉴参考之外&#xff0c;也更希望通过这篇文章呼吁大家在平时开发程序时能够更加注意程序的性能和所消耗的资源&#xff0c;避免在流量突增时给系统…

【web APIs】3、(学习笔记)有案例!

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、概念其他事件页面加载事件元素滚动事件页面尺寸事件 元素尺寸与位置 二、案例举例电梯导航 前言 掌握阻止事件冒泡的方法理解事件委托的实现原理 一、概念…

【MATLAB源码-第151期】基于matlab的开普勒化算法(KOA)无人机三维路径规划,输出做短路径图和适应度曲线。

操作环境&#xff1a; MATLAB 2022a 1、算法描述 开普勒优化算法&#xff08;Kepler Optimization Algorithm, KOA&#xff09;是一个虚构的、灵感来自天文学的优化算法&#xff0c;它借鉴了开普勒行星运动定律的概念来设计。在这个构想中&#xff0c;算法模仿行星围绕太阳的…

免费网站搭建

免费网站搭建 一.领取一个免费域名和SSL证书&#xff0c;和CDN 特点&#xff1a;支持Cloudflare CDN Cloudflare是全球知名的CDN提供商&#xff0c;如果你不想暴露你的源站&#xff0c;又想使用我们的二级域名&#xff0c;不需要前往Cloudflare添加域名&#xff0c;修改NS记录…

特斯拉掀起「端到端」风暴,自动驾驶持续开卷

作者 |三少爷 编辑 |祥威 最近&#xff0c;特斯拉向在美用户推送了版本号为V12.1.2 Beta的端到端FSD&#xff0c;版本推送后&#xff0c;海外的特斯拉车主和视频博主上传了一些测试视频&#xff0c;测评视频本身没有太多好说的&#xff0c;真正值得关注的是「端到端」。 自马…

C++ 之LeetCode刷题记录(三十五)

&#x1f604;&#x1f60a;&#x1f606;&#x1f603;&#x1f604;&#x1f60a;&#x1f606;&#x1f603; 开始cpp刷题之旅。 目标&#xff1a;执行用时击败90%以上使用 C 的用户。 15. 三数之和 给你一个整数数组 nums &#xff0c;判断是否存在三元组 [nums[i], num…

(2024,Sora 逆向工程,DiT,LVM 技术综述)Sora:大视觉模型的背景、技术、局限性和机遇回顾

Sora: A Review on Background, Technology, Limitations, and Opportunities of Large Vision Models 公和众和号&#xff1a;EDPJ&#xff08;进 Q 交流群&#xff1a;922230617 或加 VX&#xff1a;CV_EDPJ 进 V 交流群&#xff09; 目录 0. 摘要 1. 简介 2. 背景 2.1…

「算法」前缀和

前缀和主要解决求数组中某段区间元素和的问题&#xff0c;使用前缀和解决问题的步骤如下: 预处理一个前缀和数组使用这个数组 一维前缀和 现在有一个一维数组nums 预处理前缀和数组 定义一个数组 dp[]&#xff0c;dp[i]表示 nums 中 [0,i-1] 区间的元素和&#xff0c;那我们…

《互联网的世界》第二讲-最短路径优先

昨天讲 dns 时讲过&#xff0c;“你问一个当地人最近的厕所在哪&#xff0c;路人给你一个地址…”&#xff0c;可是只有地址还不够&#xff0c;如何到达那里呢&#xff1f;这是本节的内容。 自然的方式是&#xff0c;一边走一边问&#xff0c;根据路人的指示继续一边走一边问…

Cap0:TensorRT环境搭建

文章目录 1、安装TensorRT1.1、下载TensorRT压缩包1.2、配置环境变量 2、测试2.1、测试源码2.2、编译源码 1、安装TensorRT TensorRT是针对NVIDIA显卡设备的加速方案&#xff0c;你要使用TensorRT则证明你有一定的深度学习基础&#xff0c;那么在你的Ubuntu上配置好显卡驱动、…

Vue.js实战:构建一个简单的学生管理系统

摘要&#xff1a; 本文将引导你使用Vue.js框架来构建一个完整的学生管理系统。我们将从环境搭建开始&#xff0c;逐步介绍Vue的核心概念、组件创建、数据管理、事件处理、路由配置以及项目构建等关键步骤。通过实际操作&#xff0c;你将能够掌握Vue.js的基础知识&#xff0c;并…

基础!!!吴恩达deeplearning.ai:多标签分类与高级优化方法

以下内容有任何不理解可以翻看我之前的博客哦&#xff1a;吴恩达deeplearning.ai专栏 文章目录 智能驾驶高级优化方法梯度下降算法回顾 Adam算法(Adaptive Moment estimation)模型部分编译部分拟合部分 之前我们已经学习了多分类问题的神经网络的搭建。而有一种特殊的分类问题…