【八】【SQL】子查询和where

显示与SMITH同一部门的员工

 
mysql> select *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 |
+--------+-------+-------+------+---------------------+--------+------+--------+
1 row in set (0.00 sec)mysql> select deptno from emp where ename='SMITH';
+--------+
| deptno |
+--------+
|     20 |
+--------+
1 row in set (0.00 sec)mysql> select *from emp where deptno=20;
+--------+-------+---------+------+---------------------+---------+------+--------+
| 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)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)mysql> 

在这些示例中,我们看到了子查询(subquery)和WHERE子句在数据库查询中是如何结合使用的,特别是在需要基于其他查询结果进行筛选时。

查询特定员工信息: 第一个查询通过WHERE子句直接找到名为SMITH的员工信息。

查询特定员工的部门编号: 第二个查询使用WHERE子句来找到SMITHdeptno。这是一个简单的查询,返回SMITH所在的部门编号,即20

基于部门编号查询部门内所有员工: 第三个查询通过WHERE子句和硬编码的部门编号(从第二个查询中得知为20)来找到所有在该部门工作的员工。

使用子查询自动找到特定员工的部门内所有员工: 第四个查询展示了子查询的使用。这里,WHERE子句中的条件是通过子查询动态确定的,即先找到SMITHdeptno,然后使用这个结果来筛选同一部门内的所有员工。这个查询避免了硬编码部门编号,使查询更灵活和自动化。

查询和10号部门的工作岗位相同的雇员的名字,岗位,工资,部门号,但是不包括10号自己的

 
mysql> select job from emp where deptno=10;
+-----------+
| job       |
+-----------+
| MANAGER   |
| PRESIDENT |
| CLERK     |
+-----------+
3 rows in set (0.00 sec)mysql> select  distinct job from emp where deptno=10;
+-----------+
| job       |
+-----------+
| MANAGER   |
| PRESIDENT |
| CLERK     |
+-----------+
3 rows in set (0.01 sec)mysql> select ename,job ,sal ,deptno from emp where job in (select distinct job from emp where deptno=10);
+--------+-----------+---------+--------+
| ename  | job       | sal     | deptno |
+--------+-----------+---------+--------+
| JONES  | MANAGER   | 2975.00 |     20 |
| BLAKE  | MANAGER   | 2850.00 |     30 |
| CLARK  | MANAGER   | 2450.00 |     10 |
| KING   | PRESIDENT | 5000.00 |     10 |
| SMITH  | CLERK     |  800.00 |     20 |
| ADAMS  | CLERK     | 1100.00 |     20 |
| JAMES  | CLERK     |  950.00 |     30 |
| MILLER | CLERK     | 1300.00 |     10 |
+--------+-----------+---------+--------+
8 rows in set (0.00 sec)mysql> select ename,job ,sal ,deptno from emp where (job in (select distinct 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)mysql> select * from (select ename,job ,sal ,deptno from emp where (job in (select distinct job from emp where deptno=10)) and deptno <>
>10) as tmp ,dept;
+-------+---------+---------+--------+--------+------------+----------+
| ename | job     | sal     | deptno | deptno | dname      | loc      |
+-------+---------+---------+--------+--------+------------+----------+
| SMITH | CLERK   |  800.00 |     20 |     10 | ACCOUNTING | NEW YORK |
| JONES | MANAGER | 2975.00 |     20 |     10 | ACCOUNTING | NEW YORK |
| BLAKE | MANAGER | 2850.00 |     30 |     10 | ACCOUNTING | NEW YORK |
| ADAMS | CLERK   | 1100.00 |     20 |     10 | ACCOUNTING | NEW YORK |
| JAMES | CLERK   |  950.00 |     30 |     10 | ACCOUNTING | NEW YORK |
| SMITH | CLERK   |  800.00 |     20 |     20 | RESEARCH   | DALLAS   |
| JONES | MANAGER | 2975.00 |     20 |     20 | RESEARCH   | DALLAS   |
| BLAKE | MANAGER | 2850.00 |     30 |     20 | RESEARCH   | DALLAS   |
| ADAMS | CLERK   | 1100.00 |     20 |     20 | RESEARCH   | DALLAS   |
| JAMES | CLERK   |  950.00 |     30 |     20 | RESEARCH   | DALLAS   |
| SMITH | CLERK   |  800.00 |     20 |     30 | SALES      | CHICAGO  |
| JONES | MANAGER | 2975.00 |     20 |     30 | SALES      | CHICAGO  |
| BLAKE | MANAGER | 2850.00 |     30 |     30 | SALES      | CHICAGO  |
| ADAMS | CLERK   | 1100.00 |     20 |     30 | SALES      | CHICAGO  |
| JAMES | CLERK   |  950.00 |     30 |     30 | SALES      | CHICAGO  |
| SMITH | CLERK   |  800.00 |     20 |     40 | OPERATIONS | BOSTON   |
| JONES | MANAGER | 2975.00 |     20 |     40 | OPERATIONS | BOSTON   |
| BLAKE | MANAGER | 2850.00 |     30 |     40 | OPERATIONS | BOSTON   |
| ADAMS | CLERK   | 1100.00 |     20 |     40 | OPERATIONS | BOSTON   |
| JAMES | CLERK   |  950.00 |     30 |     40 | OPERATIONS | BOSTON   |
+-------+---------+---------+--------+--------+------------+----------+
20 rows in set (0.00 sec)mysql> select * from (select ename,job ,sal ,deptno from emp where (job in (select distinct job from emp where deptno=10)) and deptno <>10) as tmp ,dept where dept.deptno=tmp.deptno;
+-------+---------+---------+--------+--------+----------+---------+
| ename | job     | sal     | deptno | deptno | dname    | loc     |
+-------+---------+---------+--------+--------+----------+---------+
| SMITH | CLERK   |  800.00 |     20 |     20 | RESEARCH | DALLAS  |
| JONES | MANAGER | 2975.00 |     20 |     20 | RESEARCH | DALLAS  |
| ADAMS | CLERK   | 1100.00 |     20 |     20 | RESEARCH | DALLAS  |
| BLAKE | MANAGER | 2850.00 |     30 |     30 | SALES    | CHICAGO |
| JAMES | CLERK   |  950.00 |     30 |     30 | SALES    | CHICAGO |
+-------+---------+---------+--------+--------+----------+---------+
5 rows in set (0.00 sec)mysql> select ename,job ,sal ,dname from (select ename,job ,sal ,deptno from emp where (job in (select distinct job from emp where deptnno=10)) 
+-------+---------+---------+----------+
| ename | job     | sal     | dname    |
+-------+---------+---------+----------+
| SMITH | CLERK   |  800.00 | RESEARCH |
| JONES | MANAGER | 2975.00 | RESEARCH |
| ADAMS | CLERK   | 1100.00 | RESEARCH |
| BLAKE | MANAGER | 2850.00 | SALES    |
| JAMES | CLERK   |  950.00 | SALES    |
+-------+---------+---------+----------+
5 rows in set (0.00 sec)mysql> 

子查询和WHERE的关系

当子查询用在WHERE子句中时,它可以为外部查询提供条件值。例如,你可以使用子查询找出与某个特定条件匹配的记录,然后用这个结果去过滤外部查询的结果。

子查询通常用于比较运算符的右侧,如=, <>, IN, NOT IN, >, <, >=, <=等。

子查询可以返回单个值或多个值,具体取决于你如何使用它们。返回单个值的子查询通常用于标准的比较运算,而返回多个值的子查询常用于IN, ANY, ALL这样的操作中。

查找特定部门号的所有职位

 

SELECT job FROM emp WHERE deptno=10;

这个查询返回部门号为10的所有职位。

使用子查询过滤特定条件的记录

 

SELECT * FROM emp WHERE deptno=(SELECT deptno FROM emp WHERE ename='SMITH');

这个查询首先找出名为SMITH的员工所在的部门号,然后返回该部门所有员工的记录。

结合子查询和DISTINCT关键字

 

SELECT ename, job, sal, deptno FROM emp WHERE job IN (SELECT DISTINCT job FROM emp WHERE deptno=10);

这个查询首先找出部门号为10的所有不重复的职位,然后返回具有这些职位的所有员工的记录。

使用子查询和连接查询提供更复杂的筛选逻辑

 

SELECT * FROM (SELECT ename, job, sal, deptno FROM emp WHERE (job IN (SELECT DISTINCT job FROM emp WHERE deptno=10)) AND deptno <> 10) AS tmp, dept WHERE dept.deptno=tmp.deptno;

这个查询展示了如何使用子查询作为一个临时表(tmp)的筛选条件,并且如何将它与另一个表(dept)进行连接,以便获取满足特定条件的详细信息。

显示工资比部门30的所有员工的工资高的员工的姓名、工资和部门号

 
mysql> select max(sal) from emp;
+----------+
| max(sal) |
+----------+
|  5000.00 |
+----------+
1 row in set (0.00 sec)mysql> select max(sal) from emp where deptno =30;
+----------+
| max(sal) |
+----------+
|  2850.00 |
+----------+
1 row in set (0.00 sec)mysql> select *from emp where sal>2850;
+--------+-------+-----------+------+---------------------+---------+------+--------+
| empno  | ename | job       | mgr  | hiredate            | sal     | comm | deptno |
+--------+-------+-----------+------+---------------------+---------+------+--------+
| 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 |
| 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 |
+--------+-------+-----------+------+---------------------+---------+------+--------+
4 rows in set (0.00 sec)mysql> select *from emp where sal>(select max(sal) from emp where deptno =30);
+--------+-------+-----------+------+---------------------+---------+------+--------+
| empno  | ename | job       | mgr  | hiredate            | sal     | comm | deptno |
+--------+-------+-----------+------+---------------------+---------+------+--------+
| 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 |
| 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 |
+--------+-------+-----------+------+---------------------+---------+------+--------+
4 rows in set (0.00 sec)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)mysql> 
mysql> 
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.00 sec)mysql> select distinct sal from emp where deptno =30;
+---------+
| sal     |
+---------+
| 1600.00 |
| 1250.00 |
| 2850.00 |
| 1500.00 |
|  950.00 |
+---------+
5 rows in set (0.00 sec)mysql> select *from emp where sal >all(select distinct sal from emp where deptno =30);
+--------+-------+-----------+------+---------------------+---------+------+--------+
| empno  | ename | job       | mgr  | hiredate            | sal     | comm | deptno |
+--------+-------+-----------+------+---------------------+---------+------+--------+
| 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 |
| 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 |
+--------+-------+-----------+------+---------------------+---------+------+--------+
4 rows in set (0.00 sec)mysql> select ename,sal,deptno  from emp where sal >all(select distinct 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)mysql> 

这一系列的SQL查询展示了如何使用SELECT语句来执行数据库中的不同类型的数据检索操作,包括使用聚合函数(如MAX),子查询,以及ALL关键字来过滤结果。

第一种方法:

获取最高薪水:

SELECT max(sal) FROM emp;

此查询返回整个emp表中的最高薪水。

获取特定部门的最高薪水:

SELECT max(sal) FROM emp WHERE deptno = 30;

此查询返回部门号为30的员工中的最高薪水。

获取薪水高于2850的所有员工:

SELECT * FROM emp WHERE sal > 2850;

这个查询返回薪水高于2850的所有员工的详细信息。

使用子查询过滤薪水:

SELECT * FROM emp WHERE sal > (SELECT max(sal) FROM emp WHERE deptno = 30);

通过子查询找到部门号为30的最高薪水,然后返回薪水高于这个值的所有员工的详细信息。

第二种方法:

查询特定部门的所有不同薪资:

SELECT DISTINCT sal FROM emp WHERE deptno = 30;

这个查询返回部门号为30的所有不同薪资水平。

筛选出薪水高于部门30中所有员工薪水的员工:

SELECT * FROM emp WHERE sal > ALL (SELECT distinct sal FROM emp WHERE deptno = 30);

通过子查询找出部门号为30的所有不同的薪水值,然后返回薪水高于这些值中的任意一个的所有员工的详细信息。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.00 sec)mysql> select distinct sal from emp where deptno=30;
+---------+
| sal     |
+---------+
| 1600.00 |
| 1250.00 |
| 2850.00 |
| 1500.00 |
|  950.00 |
+---------+
5 rows in set (0.01 sec)mysql> select *from emp where sal> any (select distinct sal from emp where deptno=30);
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno  | ename  | job       | mgr  | hiredate            | sal     | comm    | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 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 |
| 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 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
12 rows in set (0.00 sec)mysql> select ename,sal,deptno from emp where sal> any (select distinct 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.00 sec)mysql> 
mysql> 
mysql> select min(sal) from emp where deptno =30;
+----------+
| min(sal) |
+----------+
|   950.00 |
+----------+
1 row in set (0.00 sec)mysql> select *from emp where sal>(select min(sal) from emp where deptno =30);
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| empno  | ename  | job       | mgr  | hiredate            | sal     | comm    | deptno |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
| 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 |
| 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 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
12 rows in set (0.00 sec)mysql> select ename,sal,deptno from emp where sal>(select min(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.00 sec)mysql> 

这一系列查询展示了如何使用ANYALL关键字以及MIN函数来筛选出满足特定条件的记录。

第一种方法:

查询特定部门的薪资列表

SELECT sal FROM emp WHERE deptno = 30;

这个查询返回部门号为30的所有员工的薪资。

查询特定部门的不同薪资值

SELECT DISTINCT sal FROM emp WHERE deptno = 30;

该查询返回部门号为30的员工的不同薪资水平。

查询薪资高于部门30中任一薪资的员工

SELECT * FROM emp WHERE sal > ANY (SELECT DISTINCT sal FROM emp WHERE deptno = 30);

这个查询返回薪资高于部门号为30中的任一薪资水平的所有员工信息。

第二种方法:

查询特定部门的最低薪资

SELECT MIN(sal) FROM emp WHERE deptno = 30;

该查询返回部门号为30的员工中最低的薪资。

查询薪资高于部门30最低薪资的员工

SELECT * FROM emp WHERE sal > (SELECT MIN(sal) FROM emp WHERE deptno = 30);

这个查询返回薪资高于部门号为30的最低薪资的所有员工信息。

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

 
mysql> select deptno,job from emp;
+--------+-----------+
| deptno | job       |
+--------+-----------+
|     20 | CLERK     |
|     30 | SALESMAN  |
|     30 | SALESMAN  |
|     20 | MANAGER   |
|     30 | SALESMAN  |
|     30 | MANAGER   |
|     10 | MANAGER   |
|     20 | ANALYST   |
|     10 | PRESIDENT |
|     30 | SALESMAN  |
|     20 | CLERK     |
|     30 | CLERK     |
|     20 | ANALYST   |
|     10 | CLERK     |
+--------+-----------+
14 rows in set (0.00 sec)mysql> select deptno,job from emp where ename='SMITH';
+--------+-------+
| deptno | job   |
+--------+-------+
|     20 | CLERK |
+--------+-------+
1 row in set (0.00 sec)mysql> select *from emp where (deptno ,job )=(select deptno ,job 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 |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL |     20 |
+--------+-------+-------+------+---------------------+---------+------+--------+
2 rows in set (0.00 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.00 sec)mysql> 
mysql> 
mysql> select *from emp where deptno=(select deptno from emp where ename='SMITH') and job=(select job 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 |
| 007876 | ADAMS | CLERK | 7788 | 1987-05-23 00:00:00 | 1100.00 | NULL |     20 |
+--------+-------+-------+------+---------------------+---------+------+--------+
2 rows in set (0.00 sec)mysql> select *from emp where deptno=(select deptno from emp where ename='SMITH') and job=(select 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.01 sec)mysql> 

目前全部的子查询,全部都是在where子句中,充当判断条件!!

任何时刻,查询出来的临时结构,本质在逻辑上也是表结构!

这一系列查询演示了如何使用组合条件和子查询来筛选特定的记录。

第一种方法:

查询所有员工的部门号和职位

SELECT deptno, job FROM emp;

返回所有员工的部门号和职位信息。

查询特定员工(例如SMITH)的部门号和职位

SELECT deptno, job FROM emp WHERE ename = 'SMITH';

返回名为SMITH的员工的部门号和职位。

查询与SMITH相同部门和职位的所有员工

SELECT * FROM emp WHERE (deptno, job) = (SELECT deptno, job FROM emp WHERE ename = 'SMITH');

使用组合条件(deptno, job)与子查询配合,返回与SMITH在同一部门且职位相同的所有员工信息。

查询除SMITH外,与SMITH相同部门和职位的所有员工

SELECT * FROM emp WHERE (deptno, job) = (SELECT deptno, job FROM emp WHERE ename = 'SMITH') AND ename <> 'SMITH';

在上一个查询的基础上增加了一个条件来排除SMITH本身,返回除SMITH外,与SMITH在同一部门且职位相同的所有员工信息。

第二种方法:

分开使用子查询条件进行筛选

SELECT * FROM emp WHERE deptno = (SELECT deptno FROM emp WHERE ename = 'SMITH') AND job = (SELECT job FROM emp WHERE ename = 'SMITH');

这个查询与第三个查询效果相同,但是分别对deptnojob使用了子查询作为条件进行筛选。

分开使用子查询条件进行筛选并排除SMITH

SELECT * FROM emp WHERE deptno = (SELECT deptno FROM emp WHERE ename = 'SMITH') AND job = (SELECT job FROM emp WHERE ename = 'SMITH') AND ename <> 'SMITH';

结尾

最后,感谢您阅读我的文章,希望这些内容能够对您有所启发和帮助。如果您有任何问题或想要分享您的观点,请随时在评论区留言。

同时,不要忘记订阅我的博客以获取更多有趣的内容。在未来的文章中,我将继续探讨这个话题的不同方面,为您呈现更多深度和见解。

谢谢您的支持,期待与您在下一篇文章中再次相遇!

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

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

相关文章

Python调用C,python call c,pybind11

文章目录 前言1.将pybind11 clone至当前项目下的extern目录下2.在CmakeLists.txt中将pybind11项目包含3.接口cpp文件格式4.编译5.导入Python使用6.性能比较pybind11项目地址 前言 通过https://github.com/pybind/pybind11项目实现Python调用C/C代码 实现步骤 1.将pybind11 cl…

腾讯云4核8G服务器申请费用多少?性能如何?支持几个人?

腾讯云4核8G服务器支持多少人在线访问&#xff1f;支持25人同时访问。实际上程序效率不同支持人数在线人数不同&#xff0c;公网带宽也是影响4核8G服务器并发数的一大因素&#xff0c;假设公网带宽太小&#xff0c;流量直接卡在入口&#xff0c;4核8G配置的CPU内存也会造成计算…

大数据报告检测到风险等级太高是怎么回事呢?

随着金融风控越来越多元化&#xff0c;大数据作为新兴的技术被运用到贷前风控中去了&#xff0c;不少人也了解过自己的大数据&#xff0c;但是由于相关知识不足&#xff0c;看不懂报告&#xff0c;在常见的问题中&#xff0c;大数据检测到风险等级太高是怎么回事呢?小易大数据…

《javascript高级程序设计》学习笔记 | 21.2.错误处理

关注[前端小讴]&#xff0c;阅读更多原创技术文章 错误处理 相关代码 → try/catch 语句 ES3 新增了try/catch语句&#xff0c;基本语法与 Java 中的 try/catch 一样 try {// 可能出错的代码const a 3;a 4; } catch (error) {// 出错时执行的代码console.log("An er…

vsomeip源码剖析--00环境搭建

环境 Win11 WSL2 Ubuntu22.04安装依赖 sudo apt-get install cmake sudo apt-get install libboost-system1.71-dev libboost-thread1.71-dev libboost-log1.71-dev源码编译 获取源码 https://github.com/COVESA/vsomeip.git编译 cd vsomeip mkdir build cd build// 一般…

漫漫数学之旅035

文章目录 经典格言数学习题古今评注名人小传 - 黎勒•笛卡尔 经典格言 完美的数和完美的人是同样罕见的。——黎勒•笛卡尔&#xff08;Ren Descrates&#xff09; 完美的数和完美的人都是极为罕见的。这句话表达了一个哲学观点&#xff0c;即无论是在数学领域还是人类自身&am…

Spring框架相关问题

RabbitMQ相关问题 Spring框架相关问题 一、Spring容器中的Bean是线程安全的吗&#xff1f;二、如何保证Spring容器中的Bean是线程安全的呢&#xff1f;三、什么情况下会触发Spring事务回滚&#xff1f;四、如果事务方法抛出IOException&#xff0c;是否会触发Spring事务回滚&a…

Zookeeper学习2:原理、常用脚本、选举机制、监听器

文章目录 原理选举机制&#xff08;重点&#xff09;情况1&#xff1a;正常启动集群情况2&#xff1a;集群启动完&#xff0c;中途有机器挂了 监听器客户端向服务端写入数据客户端向服务端Leader节点写入客户端向服务端Follower节点写入 Paxos算法&#xff08;每个节点都可以提…

AMDGPU KFD Test 编译使用

ROCT-Thunk-Interface是一个用于在ROCm软件堆栈中提供设备无关性的层。它是ROCm的一部分,允许不同的硬件平台(如AMD GPU和Intel CPU)使用相同的API进行计算。 要安装ROCT-Thunk-Interface,首先需要创建一个新的目录,并进入该目录: mkdir rocm-build cd rocm-build然后,…

ng : 无法将ng项识别为 cmdlet、函数、脚本文件或可运行程序的名称

ng : 无法将“ng”项识别为 cmdlet、函数、脚本文件或可运行程序的名称”&#xff0c;出现这种错误&#xff0c;那说明你angular-cli没有下载所以环境变量里没有相应的东西 1、需要在cmd里输入npm install -g angular/cli 2、之后运行angular命令时还可能出现这种错误 “ng : …

ruoyi 图片等文件资源读取

老是忘&#xff0c;记录一下 ResourcesConfig 文件下 /** 本地文件上传路径 */ registry.addResourceHandler(Constants.RESOURCE_PREFIX "/**").addResourceLocations("file:" RuoYiConfig.getProfile() "/"); /*** 资源映射路径 前缀*/ …

kafka消费者重平衡是什么?怎么避免?

消费者重平衡是指主题下的分区怎么分配给消费者的过程。下面这个图可以看出该过程&#xff1a;原来有2个消费者&#xff0c;3个分区&#xff0c;其中一个消费者肯定就的处理2个分区了。那么当新加入消费者时&#xff0c;则每个消费者就只处理一个分区了。处理这个分区过程的叫协…

详解Nacos注册中心的使用

文章目录 1、安装2、服务注册2.1、引入依赖2.2、配置nacos地址2.3、重启 3、服务分级存储模型3.1、给user-service配置集群3.2、同集群优先的负载均衡 4、权重配置5、环境隔离5.1、创建namespace5.2、配置namespace 6、Nacos与Eureka的区别7、代码免费分享 ​&#x1f343;作者…

首例以“冠状病毒”为主题的勒索病毒,篡改系统MBR

前言概述 2020年勒索病毒攻击仍然是网络安全的最大威胁&#xff0c;在短短三个月的时间里&#xff0c;已经出现了多款新型的勒索病毒&#xff0c;关于2020年勒索病毒攻击新趋势&#xff0c;可以阅读笔者写的上一篇文章&#xff0c;里面有详细的分析&#xff0c;从目前观察到的…

Linux 学习笔记(9)

九、 运行级别 1 、 Linux 系统的运行级别 (runlevel) Linux 系统有 7 个运行级别&#xff0c; Linux 系统任何时候都运行在一个指定的运行级别上&#xff0c;不同的运行级 别所运行的程序和服务不尽相同&#xff0c;所要完成的工作和要达到的目的也不相同 运行级别…

RH850P1X芯片学习笔记-Generic Timer Module -ATOM

文章目录 ARU-connected Timer Output Module (ATOM)OverviewGLOBAL CHANNEL CONTROL BLOCK ATOM Channel architectureATOM Channel modesSOMP-Signal Output Mode PWMSOMP - ARUSOMC-Signal Output Mode CompareSOMC - ARUSOMC – COMPARE COMMANDSOMC – OUTPUT ACTIONATOM …

Python缩进规则

Python的缩进规则是Python语法中非常重要的一部分&#xff0c;也是Python语言独特的特点之一。在Python中&#xff0c;缩进被用来表示代码块的层次结构&#xff0c;而不是像其他语言一样使用大括号或关键词。这种缩进规则使得Python代码更加简洁、易读、易于理解&#xff0c;同…

python模块百科_操作系统接口_os【一】

python模块百科_操作系统接口_os【一】 os --- 多种操作系统接口一、相关模块1.1 os.path 文件路径1.2 fileinput 文件读取1.3 tempfile 临时文件和目录1.4 shutil 高级文件和目录1.5 platform 操作系统底层模块 二、关于函数适用性的说明2.1 与操作系统相同的接口2.2 支持字节…

Git版本管理常用指令

Git常用命令 一、基本指令二、本地仓库管理三、远程仓库管理四、分支管理五、储藏区六、标签管理一、基本指令 查看Git安装版本:git --version 查看log指令的帮助信息:git log --help 配置Git用户名:git config --global user.name “xxxxx” 配置Git邮箱: git config --…

2024年腾讯云新用户优惠券领取入口及使用教程

随着云计算技术的不断发展和普及&#xff0c;越来越多的个人和企业选择使用云服务。腾讯云作为国内领先的云服务提供商&#xff0c;为了吸引新用户&#xff0c;经常推出各种优惠活动&#xff0c;其中就包括新用户专属优惠券&#xff0c;本文将为大家分享腾讯云新用户优惠券的领…