复合查询和内外连接

news2024/11/29 2:51:34

文章目录

  • 1. 简单查询
  • 2. 多表查询
    • 2.1 显示雇员名、雇员工资以及所在部门的名字
    • 2.2 显示部门号为10的部门名,员工名和工资
    • 2.3 显示各个员工的姓名,工资,及工资级别
  • 3. 自连接
  • 4. 子查询
    • 4.1 where后的子查询
      • 4.1.1 单行子查询
      • 4.1.2 多行子查询 (in, all, any)
      • 4.1.3 多列子查询
    • 4.2 from后的子查询
    • 4.3 合并查询
  • 5. 内外连接
    • 5.1 内连接
    • 5.2 左外连接
    • 5.3 右外连接

1. 简单查询

先做几个简单查询的题目,使用下面的emp表

image-20241126194302041

查询工资高于2000或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J

mysql> select * from emp where (sal > 2000 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 |
+--------+-------+---------+------+---------------------+---------+------+--------+
-- 也可以写成这样
select * from emp where (sal > 2000 or job = "MANAGER") and left(ename,1)='J';

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

mysql> select ename, deptno, sal from emp order by deptno asc, sal desc;
+--------+--------+---------+
| ename  | deptno | sal     |
+--------+--------+---------+
| KING   |     10 | 5000.00 |
| CLARK  |     10 | 2450.00 |
| MILLER |     10 | 1300.00 |
| SCOTT  |     20 | 3000.00 |
| FORD   |     20 | 3000.00 |
| JONES  |     20 | 2975.00 |
| ADAMS  |     20 | 1100.00 |
| SMITH  |     20 |  800.00 |
| BLAKE  |     30 | 2850.00 |
| ALLEN  |     30 | 1600.00 |
| TURNER |     30 | 1500.00 |
| WARD   |     30 | 1250.00 |
| MARTIN |     30 | 1250.00 |
| JAMES  |     30 |  950.00 |
+--------+--------+---------+
14 rows in set (0.00 sec)

使用年薪进行降序排序

-- 年薪计算用sal*12+comm,由于有的列comm为null,所以这里要检测一下
mysql> select ename, sal+ifnull(comm,0) total from emp order by total desc;
+--------+---------+
| ename  | total   |
+--------+---------+
| KING   | 5000.00 |
| SCOTT  | 3000.00 |
| FORD   | 3000.00 |
| JONES  | 2975.00 |
| BLAKE  | 2850.00 |
| MARTIN | 2650.00 |
| CLARK  | 2450.00 |
| ALLEN  | 1900.00 |
| WARD   | 1750.00 |
| TURNER | 1500.00 |
| MILLER | 1300.00 |
| ADAMS  | 1100.00 |
| JAMES  |  950.00 |
| SMITH  |  800.00 |
+--------+---------+
14 rows in set (0.00 sec)

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

mysql> select max(sal) from emp;
+----------+
| max(sal) |
+----------+
|  5000.00 |
+----------+
1 row in set (0.00 sec)

mysql> select ename, sal, deptno from emp where sal = 5000;
+-------+---------+--------+
| ename | sal     | deptno |
+-------+---------+--------+
| KING  | 5000.00 |     10 |
+-------+---------+--------+
1 row in set (0.00 sec)

-- 可以嵌套查询
mysql> select ename, sal, deptno from emp where sal = (select max(sal) from emp);
+-------+---------+--------+
| ename | sal     | deptno |
+-------+---------+--------+
| KING  | 5000.00 |     10 |
+-------+---------+--------+
1 row in set (0.00 sec)

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

mysql> select empno, ename, job, sal from emp where sal > (select avg(sal) from emp);
+--------+-------+-----------+---------+
| empno  | ename | job       | sal     |
+--------+-------+-----------+---------+
| 007566 | JONES | MANAGER   | 2975.00 |
| 007698 | BLAKE | MANAGER   | 2850.00 |
| 007782 | CLARK | MANAGER   | 2450.00 |
| 007788 | SCOTT | ANALYST   | 3000.00 |
| 007839 | KING  | PRESIDENT | 5000.00 |
| 007902 | FORD  | ANALYST   | 3000.00 |
+--------+-------+-----------+---------+
6 rows in set (0.00 se

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

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

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

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

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

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

2. 多表查询

现在又有多出来的两张表,进行多表查询

image-20241126211859587

image-20241126211917125

2.1 显示雇员名、雇员工资以及所在部门的名字

显然要用到emp表和dept表,下面的操作叫做求两个表的笛卡尔积
从第一张表取出每一条记录,和第二张表的所有记录进行组合。本质是进行穷举

mysql> select * from emp, dept;
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
| empno  | ename  | job       | mgr  | hiredate            | sal     | comm    | deptno | deptno | dname      | loc      |
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
| 007369 | SMITH  | CLERK     | 7902 | 1980-12-17 00:00:00 | 800.00  | NULL    |     20 |     10 | ACCOUNTING | NEW YORK |
| 007369 | SMITH  | CLERK     | 7902 | 1980-12-17 00:00:00 | 800.00  | NULL    |     20 |     20 | RESEARCH   | DALLAS   |
| 007369 | SMITH  | CLERK     | 7902 | 1980-12-17 00:00:00 | 800.00  | NULL    |     20 |     30 | SALES      | CHICAGO  |
| 007369 | SMITH  | CLERK     | 7902 | 1980-12-17 00:00:00 | 800.00  | NULL    |     20 |     40 | OPERATIONS | BOSTON   |
| 007499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00  |     30 |     10 | ACCOUNTING | NEW YORK |
| 007499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00  |     30 |     20 | RESEARCH   | DALLAS   |
| 007499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00  |     30 |     30 | SALES      | CHICAGO  |
| 007499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 00:00:00 | 1600.00 | 300.00  |     30 |     40 | OPERATIONS | BOSTON   |
| 007521 | WARD   | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00  |     30 |     10 | ACCOUNTING | NEW YORK |
| 007521 | WARD   | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00  |     30 |     20 | RESEARCH   | DALLAS   |
| 007521 | WARD   | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00  |     30 |     30 | SALES      | CHICAGO  |
| 007521 | WARD   | SALESMAN  | 7698 | 1981-02-22 00:00:00 | 1250.00 | 500.00  |     30 |     40 | OPERATIONS | BOSTON   |
| 007566 | JONES  | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL    |     20 |     10 | ACCOUNTING | NEW YORK |
| 007566 | JONES  | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL    |     20 |     20 | RESEARCH   | DALLAS   |
| 007566 | JONES  | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL    |     20 |     30 | SALES      | CHICAGO  |
| 007566 | JONES  | MANAGER   | 7839 | 1981-04-02 00:00:00 | 2975.00 | NULL    |     20 |     40 | OPERATIONS | BOSTON   |
| 007654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |     10 | ACCOUNTING | NEW YORK |
| 007654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |     20 | RESEARCH   | DALLAS   |
| 007654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |     30 | SALES      | CHICAGO  |
| 007654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 00:00:00 | 1250.00 | 1400.00 |     30 |     40 | OPERATIONS | BOSTON   |
| 007698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL    |     30 |     10 | ACCOUNTING | NEW YORK |
| 007698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL    |     30 |     20 | RESEARCH   | DALLAS   |
| 007698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL    |     30 |     30 | SALES      | CHICAGO  |
| 007698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 00:00:00 | 2850.00 | NULL    |     30 |     40 | OPERATIONS | BOSTON   |
......
+--------+--------+-----------+------+---------------------+---------+---------+--------+--------+------------+----------+
56 rows in set (1.22 sec)

显然有重复显示的,例如对于SMITH来讲,只需要显示dept.deptno=20的那一行,所以需要加限制条件

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

按照题目要求进行显示

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      |
| CLARK  | 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.15 sec)

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

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

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

这里要用到salgrade

mysql> select ename, sal, grade, losal, hisal from emp, salgrade hisal where sal between losal and hisal;
+--------+---------+-------+-------+-------+
| ename  | sal     | grade | losal | hisal |
+--------+---------+-------+-------+-------+
| SMITH  | 800.00  |     1 |   700 |  1200 |
| ALLEN  | 1600.00 |     3 |  1401 |  2000 |
| WARD   | 1250.00 |     2 |  1201 |  1400 |
| JONES  | 2975.00 |     4 |  2001 |  3000 |
| MARTIN | 1250.00 |     2 |  1201 |  1400 |
| BLAKE  | 2850.00 |     4 |  2001 |  3000 |
| CLARK  | 2450.00 |     4 |  2001 |  3000 |
| SCOTT  | 3000.00 |     4 |  2001 |  3000 |
| KING   | 5000.00 |     5 |  3001 |  9999 |
| TURNER | 1500.00 |     3 |  1401 |  2000 |
| ADAMS  | 1100.00 |     1 |   700 |  1200 |
| JAMES  | 950.00  |     1 |   700 |  1200 |
| FORD   | 3000.00 |     4 |  2001 |  3000 |
| MILLER | 1300.00 |     2 |  1201 |  1400 |
+--------+---------+-------+-------+-------+
14 rows in set (0.12 sec)

3. 自连接

也可以自己连接自己,用dept表实例

-- 这样是不行的
mysql> select * from dept, dept;
1066 - Not unique table/alias: 'dept'

-- 需要取别名
mysql> select * from dept t1, dept t2;
+--------+------------+----------+--------+------------+----------+
| deptno | dname      | loc      | deptno | dname      | loc      |
+--------+------------+----------+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |     10 | ACCOUNTING | NEW YORK |
|     30 | SALES      | CHICAGO  |     10 | ACCOUNTING | NEW YORK |
|     40 | OPERATIONS | BOSTON   |     10 | ACCOUNTING | NEW YORK |
|     10 | ACCOUNTING | NEW YORK |     20 | RESEARCH   | DALLAS   |
|     20 | RESEARCH   | DALLAS   |     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |     20 | RESEARCH   | DALLAS   |
|     40 | OPERATIONS | BOSTON   |     20 | RESEARCH   | DALLAS   |
|     10 | ACCOUNTING | NEW YORK |     30 | SALES      | CHICAGO  |
|     20 | RESEARCH   | DALLAS   |     30 | SALES      | CHICAGO  |
|     30 | SALES      | CHICAGO  |     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |     30 | SALES      | CHICAGO  |
|     10 | ACCOUNTING | NEW YORK |     40 | OPERATIONS | BOSTON   |
|     20 | RESEARCH   | DALLAS   |     40 | OPERATIONS | BOSTON   |
|     30 | SALES      | CHICAGO  |     40 | OPERATIONS | BOSTON   |
|     40 | OPERATIONS | BOSTON   |     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+--------+------------+----------+
16 rows in set (0.15 sec)

显示员工FORD的上级领导的编号和姓名

-- 可以使用子查询
mysql> select empno, ename from emp where empno=(select mgr from emp where ename='FORD');
+--------+-------+
| empno  | ename |
+--------+-------+
| 007566 | JONES |
+--------+-------+
1 row in set (0.13 sec)
-- 自连接
mysql> select e2.empno, e2.ename from emp e1, emp e2 where e1.mgr=e2.empno and e1.ename='FORD';
+--------+-------+
| empno  | ename |
+--------+-------+
| 007566 | JONES |
+--------+-------+
1 row in set (0.14 sec)

4. 子查询

子查询(Subquery)也被称为嵌套查询(Nested Query)。它是一个嵌套在另一个 SQL 查询(通常是SELECT语句)中的完整 SQL 查询。

4.1 where后的子查询

4.1.1 单行子查询

子查询返回一行记录

显示SMITH同一部门的员工

-- 返回一行记录
mysql> select deptno from emp where ename = 'SMITH';
+--------+
| deptno |
+--------+
|     20 |
+--------+
1 row in set (0.06 sec)

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.07 sec)

4.1.2 多行子查询 (in, all, any)

子查询返回多行记录

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

-- 返回多行记录
mysql> select job from emp where deptno = 10;
+-----------+
| job       |
+-----------+
| MANAGER   |
| PRESIDENT |
| CLERK     |
+-----------+
3 rows in set (0.08 sec)

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.07 sec)

注:如果想要加上部门名称,可以这样写,就是上面的多表查询

mysql> select ename, job, sal, emp.deptno, dname from emp, dept where emp.deptno=dept.deptno and job in (select job from emp where emp.deptno = 10) and emp.deptno != 10;
+-------+---------+---------+--------+----------+
| ename | job     | sal     | deptno | dname    |
+-------+---------+---------+--------+----------+
| SMITH | CLERK   | 800.00  |     20 | RESEARCH |
| JONES | MANAGER | 2975.00 |     20 | RESEARCH |
| ADAMS | CLERK   | 1100.00 |     20 | RESEARCH |
| BLAKE | MANAGER | 2850.00 |     30 | SALES    |
| JAMES | CLERK   | 950.00  |     30 | SALES    |
+-------+---------+---------+--------+----------+
5 rows in set (0.08 sec)

-- 实际上,from后面也可以跟子查询,因为子查询返回的是一个完整的表, 下面返回结果与上面是一样的
select ename, job, sal, tmp.deptno, dname from (select ename, job, sal, deptno from emp where job in (select job from emp where deptno = 10) and deptno != 10) as tmp, dept where dept.deptno = tmp.deptno;

使用all关键字显示工资比部门30的所有员工的工资高的员工的姓名、工资和部门号

mysql> select sal from emp where deptno = 30;
+---------+
| sal     |
+---------+
| 1600.00 |
| 1250.00 |
| 1250.00 |
| 2850.00 |
| 1500.00 |
| 950.00  |
+---------+
6 rows in set (0.10 sec)

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.10 sec)

-- 当然也可以这样写
mysql> select ename, sal, deptno from emp where sal > (select max(sal) from emp where deptno = 30);

使用any关键字: 显示工资比部门30的任意员工的工资高的员工的姓名、工资和部门号(包含自己部门的员工)

mysql> select ename, sal, deptno from emp where sal > any(select sal from emp where deptno = 30);
+--------+---------+--------+
| ename  | sal     | deptno |
+--------+---------+--------+
| ALLEN  | 1600.00 |     30 |
| WARD   | 1250.00 |     30 |
| JONES  | 2975.00 |     20 |
| MARTIN | 1250.00 |     30 |
| BLAKE  | 2850.00 |     30 |
| CLARK  | 2450.00 |     10 |
| SCOTT  | 3000.00 |     20 |
| KING   | 5000.00 |     10 |
| TURNER | 1500.00 |     30 |
| ADAMS  | 1100.00 |     20 |
| FORD   | 3000.00 |     20 |
| MILLER | 1300.00 |     10 |
+--------+---------+--------+
12 rows in set (0.11 sec)

4.1.3 多列子查询

子查询返回多列记录

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

mysql> select deptno, job from emp where ename = 'SMITH';
+--------+-------+
| deptno | job   |
+--------+-------+
|     20 | CLERK |
+--------+-------+
1 row in set (0.13 sec)

mysql> select * from emp where (deptno, job) = (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.14 sec)

-- 这里也可用in,这样就是多行,多列子查询了
select * from emp where (deptno, job) = (select deptno, job from emp where ename = 'SMITH') and ename != 'SMITH';

4.2 from后的子查询

子查询语句出现在from子句中,把一个子查询当做一个临时表使用。

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

mysql> select deptno, avg(sal) avg from emp group by deptno;
+--------+-------------+
| deptno | avg         |
+--------+-------------+
|     10 | 2916.666667 |
|     20 | 2175.000000 |
|     30 | 1566.666667 |
+--------+-------------+
3 rows in set (0.11 sec)

mysql> select ename, t1.deptno, sal, avg from (select deptno, avg(sal) avg from emp group by deptno) t1, emp t2 where t1.deptno = t2.deptno and sal > avg;
+-------+--------+---------+-------------+
| ename | deptno | sal     | avg         |
+-------+--------+---------+-------------+
| 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.10 sec)

-- 如果想要查询部门名称,需要再将上面的结果放到from子句后面,再套一层。不过嵌套太多了,可读性很差
mysql> select ename, t3.deptno, sal, avg, t4.dname from (select ename, t1.deptno, sal, avg from (select deptno, avg(sal) avg from emp group by deptno) t1, emp t2 where t1.deptno = t2.deptno and sal > avg) as t3, dept as t4 where t3.deptno = t4.deptno;
+-------+--------+---------+-------------+------------+
| ename | deptno | sal     | avg         | dname      |
+-------+--------+---------+-------------+------------+
| ALLEN |     30 | 1600.00 | 1566.666667 | SALES      |
| JONES |     20 | 2975.00 | 2175.000000 | RESEARCH   |
| BLAKE |     30 | 2850.00 | 1566.666667 | SALES      |
| SCOTT |     20 | 3000.00 | 2175.000000 | RESEARCH   |
| KING  |     10 | 5000.00 | 2916.666667 | ACCOUNTING |
| FORD  |     20 | 3000.00 | 2175.000000 | RESEARCH   |
+-------+--------+---------+-------------+------------+
6 rows in set (0.11 sec)

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

mysql> select max(sal) as max, deptno from emp group by deptno;
+---------+--------+
| max     | deptno |
+---------+--------+
| 5000.00 |     10 |
| 3000.00 |     20 |
| 2850.00 |     30 |
+---------+--------+
3 rows in set (0.12 sec)

mysql> select ename, sal, t1.deptno, max from emp as t1, (select max(sal) as max, deptno from emp group by deptno) as t2 where t1.deptno=t2.deptno and t1.sal = t2.max;
+-------+---------+--------+---------+
| ename | sal     | deptno | max     |
+-------+---------+--------+---------+
| 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.13 sec)

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

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

-- 使用多表查询,由于mysql的限制,所以group by后面要加上这些多余的限制条件
mysql> select dept.deptno, dname, loc, count(*) cnt from emp, dept where emp.deptno = dept.deptno group by dept.deptno, dname, loc;

4.3 合并查询

两个关键字: unionunion allunionselect语句后的执行结果取并集,union all不做去重操作

将工资大于2500或职位是MANAGER的人找出来

mysql> select * from emp where sal > 2500 union select * from emp where job = 'MANGER';
+-------+-------+-----------+------+---------------------+---------+------+--------+
| 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 |
+-------+-------+-----------+------+---------------------+---------+------+--------+
5 rows in set (0.11 sec)

mysql> select * from emp where sal > 2500 union all select * from emp where job = 'MANGER';
+-------+-------+-----------+------+---------------------+---------+------+--------+
| 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 |
+-------+-------+-----------+------+---------------------+---------+------+--------+
5 rows in set (0.12 sec)

-- 注意: select 后面需要有相同的列
mysql> select * from emp where sal > 2500 union all select ename from emp where job = 'MANGER';
1222 - The used SELECT statements have a different number of columns

5. 内外连接

5.1 内连接

内连接可以看作是对笛卡尔积结果集的筛选。
从实现的角度来看,数据库在执行内连接时,可能会先计算两个表的笛卡尔积(在内部实现中,这可能是一种优化后的计算方式),然后根据连接条件(如ON子句指定的条件)来过滤掉不符合要求的行组合,最终得到内连接的结果。

语法:

select 字段 from 表1 inner join 表2 on 连接条件 and 其他条件;

显示SMITH的名字和部门名称

-- 使用前面笛卡尔积加筛选条件的写法
mysql> select ename, dname from emp, dept where emp.deptno=dept.deptno and emp.ename = 'SMITH';
+-------+----------+
| ename | dname    |
+-------+----------+
| SMITH | RESEARCH |
+-------+----------+
1 row in set (0.13 sec)

-- 内连接
mysql> select ename, dname from emp inner join dept on emp.deptno = dept.deptno and ename = 'SMITH';
+-------+----------+
| ename | dname    |
+-------+----------+
| SMITH | RESEARCH |
+-------+----------+
1 row in set (0.12 sec)
-- 注,上面的and也可以换成where,意味着先分好一个表后,再按where后的条件筛选

5.2 左外连接

返回左表(在FROM子句中位于LEFT JOIN关键字左边的表)中的所有行,以及右表(在LEFT JOIN关键字右边的表)中与左表连接条件匹配的行。如果右表中没有匹配的行,则在结果集中相应的列将填充为NULL值。

语法:

select 字段名 from 表名1 left join 表名2 on 连接条件

假设有下面两张表

mysql> select * from stu;
+----+------+
| id | name |
+----+------+
|  1 | jack |
|  2 | tom  |
|  3 | kity |
|  4 | nono |
+----+------+
4 rows in set (0.11 sec)

mysql> select * from exam;
+----+-------+
| id | grade |
+----+-------+
|  1 |    56 |
|  2 |    76 |
| 11 |     8 |
+----+-------+
3 rows in set (0.11 sec)

查询所有学生的成绩,如果这个学生没有成绩,也要将学生的个人信息显示出来

mysql> select * from stu left join exam on stu.id = exam.id;
+----+------+------+-------+
| id | name | id   | grade |
+----+------+------+-------+
|  1 | jack |    1 |    56 |
|  2 | tom  |    2 |    76 |
|  3 | kity | NULL | NULL  |
|  4 | nono | NULL | NULL  |
+----+------+------+-------+
4 rows in set (0.13 sec)

5.3 右外连接

返回右表(在FROM子句中位于RIGHT JOIN关键字右边的表)中的所有行,以及左表(在RIGHT JOIN关键字左边的表)中与右表连接条件匹配的行。如果左表中没有匹配的行,在结果集中相应的列会填充为NULL值。

语法:

select 字段名 from 表名1 right join 表名2 on 连接条件

继续使用5.3中的表

对stu表和exam表联合查询,把所有的成绩都显示出来,即使这个成绩没有学生与它对应,也要显示出来

mysql> select * from exam right join stu on exam.id = stu.id;
+------+-------+----+------+
| id   | grade | id | name |
+------+-------+----+------+
|    1 |    56 |  1 | jack |
|    2 |    76 |  2 | tom  |
| NULL | NULL  |  3 | kity |
| NULL | NULL  |  4 | nono |
+------+-------+----+------+
4 rows in set (0.11 sec)

使用emp表和dept表,列出部门名称和这些部门的员工信息,同时列出没有员工的部

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

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

UniApp开发实战:常见报错解析与解决方案

UniApp开发实战&#xff1a;常见报错解析与解决方案 病例1、TypeError: undefined is not an object (evaluating ‘this. s c o p e . scope. scope.getAppWebview’) 需求&#xff1a;获取页面示例&#xff0c;动态修改头部搜索框内容&#xff0c;获取页面实例时候报错unde…

Docker 容器网络创建网桥链接

一、网络&#xff1a;默认情况下&#xff0c;所有的容器都以bridge方式链接到docker的一个虚拟网桥上&#xff1b; 注意&#xff1a;“172.17.0.0/16”中的“/16”表示子网掩码的长度为16位&#xff0c;它表示子网掩码中有16个连续的1&#xff0c;后面跟着16个连续的0。用于区分…

一个开源轻量级的服务器资源监控平台,支持告警推送

大家好&#xff0c;今天给大家分享一款开源的轻量级服务器资源监控工具Beszel&#xff0c;提供历史数据记录、Docker容器统计信息监控以及多种警报功能&#xff0c;用于监控服务器资源。 项目介绍 Beszel由hub&#xff08;中心服务器端应用&#xff0c;基于PocketBase构建&…

使用Compose Multiplatform开发跨平台的Android调试工具

背景 最近对CMP跨平台很感兴趣&#xff0c;为了练手&#xff0c;在移动端做了一个Android和IOS共享UI和逻辑代码的天气软件&#xff0c;简单适配了一下双端的深浅主题切换&#xff0c;网络状态监测&#xff0c;刷新调用振动器接口。 做了两年多车机Android开发&#xff0c;偶…

[MRCTF2020]Transform

查壳&#xff0c;拖入64位IDA LOBYTE8位就是一个字节&#xff0c;在此处无意义&#xff0c;因为我们输入的本来就是按字节输入的 设 a byte_414040,bdword_40F040,cbyte_40F0E0,输入的字符串为flag; 从题目里得到 加密代码 a[i] flag[b[i]]; a[i] ^ b[i]; c a 即c[i] a[i…

podman 源码 5.3.1编译

1. 构建环境 在麒麟V10服务器操作系统上构建&#xff1a;Kylin-Server-V10-GFB-Release-2204-Build03-ARM64.iso。由于只是编译 podman 源码&#xff0c;没必要特地在物理机或服务上安装一个这样的操作系统&#xff0c;故采用在虚拟机里验证。 2. 安装依赖 参考资料&#xf…

Llmcad: Fast and scalable on-device large language model inference

题目&#xff1a;Llmcad: Fast and scalable on-device large language model inference 发表于2023.09 链接&#xff1a;https://arxiv.org/pdf/2309.04255 声称是第一篇speculative decoding边缘设备的论文&#xff08;不一定是绝对的第一篇&#xff09;&#xff0c;不开源…

用Java爬虫“搜刮”工厂数据:一场数据的寻宝之旅

引言&#xff1a;数据的宝藏 在这个数字化的时代&#xff0c;数据就像是隐藏在数字丛林中的宝藏&#xff0c;等待着勇敢的探险家去发掘。而我们&#xff0c;就是那些手持Java魔杖的现代海盗&#xff0c;准备用我们的爬虫船去征服那些数据的海洋。今天&#xff0c;我们将一起踏…

14、保存与加载PyTorch训练的模型和超参数

文章目录 1. state_dict2. 模型保存3. check_point4. 详细保存5. Docker6. 机器学习常用库 1. state_dict nn.Module 类是所有神经网络构建的基类&#xff0c;即自己构建一个深度神经网络也是需要继承自nn.Module类才行&#xff0c;并且nn.Module中的state_dict包含神经网络中…

【计算机网络】多路转接之poll

poll也是一种linux中的多路转接方案(poll也是只负责IO过程中的"等") 解决&#xff1a;1.select的fd有上限的问题&#xff1b;2.每次调用都要重新设置关心的fd 一、poll的使用 int poll(struct pollfd *fds, nfds_t nfds, int timeout); ① struct pollfd *fds&…

矩阵重新排列——sort函数

s o r t sort sort函数表示排序&#xff0c;对向量和矩阵都成立 向量 s o r t ( a ) sort(a) sort(a)将向量 a a a中元素从小到大排序 s o r t ( a , ′ d e s c e n d ′ ) sort(a,descend) sort(a,′descend′)将向量 a a a中元素从大到小排序 [ s o r t a , i d ] s o r…

深入解密 K 均值聚类:从理论基础到 Python 实践

1. 引言 在机器学习领域&#xff0c;聚类是一种无监督学习的技术&#xff0c;用于将数据集分组成若干个类别&#xff0c;使得同组数据之间具有更高的相似性。这种技术在各个领域都有广泛的应用&#xff0c;比如客户细分、图像压缩和市场分析等。聚类的目标是使得同类样本之间的…

Leetcode322.零钱兑换(HOT100)

链接 代码&#xff1a; class Solution { public:int coinChange(vector<int>& coins, int amount) {vector<int> dp(amount1,amount1);//要兑换amount元硬币&#xff0c;我们就算是全选择1元的硬币&#xff0c;也不过是amount个&#xff0c;所以初始化amoun…

【61-70期】Java面试题深度解析:从集合框架到线程安全的最佳实践

&#x1f680; 作者 &#xff1a;“码上有前” &#x1f680; 文章简介 &#xff1a;Java &#x1f680; 欢迎小伙伴们 点赞&#x1f44d;、收藏⭐、留言&#x1f4ac; 文章题目&#xff1a;Java面试题深度解析&#xff1a;从集合框架到线程安全的最佳实践 摘要&#xff1a; 本…

关闭AWS账号后,服务是否仍会继续运行?

在使用亚马逊网络服务&#xff08;AWS&#xff09;时&#xff0c;用户有时可能会考虑关闭自己的AWS账户。这可能是因为项目结束、费用过高&#xff0c;或是转向使用其他云服务平台。然而&#xff0c;许多人对关闭账户后的服务状态感到困惑&#xff0c;我们九河云和大家一起探讨…

JVM_垃圾收集器详解

1、 前言 JVM就是Java虚拟机&#xff0c;说白了就是为了屏蔽底层操作系统的不一致而设计出来的一个虚拟机&#xff0c;让用户更加专注上层&#xff0c;而不用在乎下层的一个产品。这就是JVM的跨平台&#xff0c;一次编译&#xff0c;到处运行。 而JVM中的核心功能其实就是自动…

若依框架部署在网站一个子目录下(/admin)问题(

部署在子目录下首先修改vue.config.js文件&#xff1a; 问题一&#xff1a;登陆之后跳转到了404页面问题&#xff0c;解决办法如下&#xff1a; src/router/index.js 把404页面直接变成了首页&#xff08;大佬有啥优雅的解决办法求告知&#xff09; 问题二&#xff1a;退出登录…

BERT解析

BERT项目 我在BERT添加注释和部分推理代码 main.py vocab WordVocab.load_vocab(args.vocab_path)#加载vocab那么这个加载的二进制是什么呢&#xff1f; 1. 加载数据集 继承关系&#xff1a;TorchVocab --> Vocab --> WordVocab TorchVocab 该类主要是定义了一个词…

连接共享打印机0X0000011B错误多种解决方法

打印机故障一直是一个热门话题&#xff0c;特别是共享打印机0x0000011b错误特别头疼&#xff0c;有很多网友经常遇到共享打印机0x0000011b错误。0x0000011b有更新补丁导致的、有访问共享打印机服务异常、有访问共享打印机驱动异常等问题导致的&#xff0c;针对共享打印机0x0000…

spring +fastjson 的 rce

前言 众所周知&#xff0c;spring 下是不可以上传 jsp 的木马来 rce 的&#xff0c;一般都是控制加载 class 或者 jar 包来 rce 的&#xff0c;我们的 fastjson 的高版本正好可以完成这些&#xff0c;这里来简单分析一手 环境搭建 <dependency><groupId>org.spr…