显示与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
子句来找到SMITH
的deptno
。这是一个简单的查询,返回SMITH
所在的部门编号,即20
。
基于部门编号查询部门内所有员工: 第三个查询通过WHERE
子句和硬编码的部门编号(从第二个查询中得知为20
)来找到所有在该部门工作的员工。
使用子查询自动找到特定员工的部门内所有员工: 第四个查询展示了子查询的使用。这里,WHERE
子句中的条件是通过子查询动态确定的,即先找到SMITH
的deptno
,然后使用这个结果来筛选同一部门内的所有员工。这个查询避免了硬编码部门编号,使查询更灵活和自动化。
查询和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>
这一系列查询展示了如何使用ANY
和ALL
关键字以及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');
这个查询与第三个查询效果相同,但是分别对deptno
和job
使用了子查询作为条件进行筛选。
分开使用子查询条件进行筛选并排除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';
结尾
最后,感谢您阅读我的文章,希望这些内容能够对您有所启发和帮助。如果您有任何问题或想要分享您的观点,请随时在评论区留言。
同时,不要忘记订阅我的博客以获取更多有趣的内容。在未来的文章中,我将继续探讨这个话题的不同方面,为您呈现更多深度和见解。
谢谢您的支持,期待与您在下一篇文章中再次相遇!