📢博客主页:https://blog.csdn.net/2301_779549673
📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
📢本文由 JohnKi 原创,首发于 CSDN🙉
📢未来很长,值得我们全力奔赴更美好的生活✨
文章目录
- 📢前言
- [🏳️🌈1 基本查询回顾](#🏳️🌈1 基本查询回顾)
- [🏳️🌈2 多表查询](#🏳️🌈2 多表查询)
- [🏳️🌈3 自连接](#🏳️🌈3 自连接)
- [🏳️🌈4 子查询](#🏳️🌈4 子查询)
-
- [❤️4.1 单行子查询](#❤️4.1 单行子查询)
- [🧡4.2 多行子查询](#🧡4.2 多行子查询)
- [💛4.3 多列子查询](#💛4.3 多列子查询)
- [💚4.4 在from子句中使用子查询](#💚4.4 在from子句中使用子查询)
- [💙4.5 合并查询](#💙4.5 合并查询)
-
- [4.5.1 union](#4.5.1 union)
- [4.5.3 union all](#4.5.3 union all)
- 👥总结
📢前言
MySQL 复合函数是一组强大的数据处理工具,可以对数据库中的数据进行统计、计算和分组等操作。这些函数包括聚合函数、流程控制语句和复合查询等,可以帮助开发人员更高效地处理数据。
MySQL 复合函数与 GROUP BY 的关系在使用 MySQL 进行数据处理和查询时,复合函数(如聚合函数)是一种常见的功能。复合函数可用于汇总数据,比如计算总和、平均数、最大值、最小值等。那么,在使用这些函数时,是否必须使用 GROUP BY 语句呢?本文将对此进行深入探讨,并提供代码示例以帮助理解。
本文用到的数据表
cpp
// emp表
mysql> show create table emp;
+-------+--------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------+
| emp | CREATE TABLE `emp` (
`empno` int(6) unsigned zerofill NOT NULL COMMENT '雇员编号',
`ename` varchar(10) DEFAULT NULL COMMENT '雇员姓名',
`job` varchar(9) DEFAULT NULL COMMENT '雇员职位',
`mgr` int(4) unsigned zerofill DEFAULT NULL COMMENT '雇员领导编号',
`hiredate` datetime DEFAULT NULL COMMENT '雇佣时间',
`sal` decimal(7,2) DEFAULT NULL COMMENT '工资月薪',
`comm` decimal(7,2) DEFAULT NULL COMMENT '奖金',
`deptno` int(2) unsigned zerofill DEFAULT NULL COMMENT '部门编号'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
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)
// // dept
mysql> show create table dept;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| dept | CREATE TABLE `dept` (
`deptno` int(2) unsigned zerofill NOT NULL COMMENT '部门编号',
`dname` varchar(14) DEFAULT NULL COMMENT '部门名称',
`loc` varchar(13) DEFAULT NULL COMMENT '部门所在地点'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row 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)
// salgrade
mysql> show create table salgrade;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| salgrade | CREATE TABLE `salgrade` (
`grade` int(11) DEFAULT NULL COMMENT '等级',
`losal` int(11) DEFAULT NULL COMMENT '此等级最低工资',
`hisal` int(11) DEFAULT NULL COMMENT '此等级最高工资'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row 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)
🏳️🌈1 基本查询回顾
查询工资高于500或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J
cpp
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)
按照部门号升序而雇员的工资降序排序
cpp
mysql> select * from emp order by deptno, 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 | CLARK | 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 |
+--------+--------+-----------+------+---------------------+---------+---------+--------+
14 rows in set (0.00 sec)
使用年薪进行降序排序
cpp
mysql> select ename, sal*12+ifnull(comm,0) as '年薪' from emp order by 年薪 desc;
+--------+----------+
| ename | 年薪 |
+--------+----------+
| KING | 60000.00 |
| SCOTT | 36000.00 |
| FORD | 36000.00 |
| JONES | 35700.00 |
| BLAKE | 34200.00 |
| CLARK | 29400.00 |
| ALLEN | 19500.00 |
| TURNER | 18000.00 |
| MARTIN | 16400.00 |
| MILLER | 15600.00 |
| WARD | 15500.00 |
| ADAMS | 13200.00 |
| JAMES | 11400.00 |
| SMITH | 9600.00 |
+--------+----------+
14 rows in set (0.00 sec)
显示工资最高的员工的名字和工作岗位
cpp
// 子查询
mysql> select ename, job, sal from emp where sal = (select(max(sal)) from emp);
+-------+-----------+---------+
| ename | job | sal |
+-------+-----------+---------+
| KING | PRESIDENT | 5000.00 |
+-------+-----------+---------+
1 row in set (0.00 sec)
显示工资高于平均工资的员工信息
cpp
// 子查询
mysql> select * from emp where sal > (select avg(sal) from emp);
+--------+-------+-----------+------+---------------------+---------+------+--------+
| 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 | 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 |
| 007902 | FORD | ANALYST | 7566 | 1981-12-03 00:00:00 | 3000.00 | NULL | 20 |
+--------+-------+-----------+------+---------------------+---------+------+--------+
6 rows in set (0.00 sec)
显示每个部门的平均工资和最高工资
cpp
mysql> select deptno, format(avg(sal), 2) , max(sal) from emp group by deptno;
+--------+---------------------+----------+
| deptno | format(avg(sal), 2) | max(sal) |
+--------+---------------------+----------+
| 10 | 2,916.67 | 5000.00 |
| 20 | 2,175.00 | 3000.00 |
| 30 | 1,566.67 | 2850.00 |
+--------+---------------------+----------+
3 rows in set (0.00 sec)
显示平均工资低于2000的部门号和它的平均工资
cpp
mysql> select deptno, avg(sal) 平均 from emp group by deptno;
+--------+-------------+
| deptno | 平均 |
+--------+-------------+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
+--------+-------------+
3 rows in set (0.01 sec)
mysql> select deptno, avg(sal) 平均 from emp group by deptno having avg(sal) < 2000;
+--------+-------------+
| deptno | 平均 |
+--------+-------------+
| 30 | 1566.666667 |
+--------+-------------+
1 row in set (0.00 sec)
显示每种岗位的雇员总数,平均工资
cpp
mysql> select job, count(*), avg(sal) 平均工资 from emp group by job;
+-----------+----------+--------------+
| job | count(*) | 平均工资 |
+-----------+----------+--------------+
| 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表,因此要联合查询
cpp
mysql> select emp.ename, emp.sal, emp.deptno, dept.dname from emp, dept where emp.deptno = dept.deptno;
+--------+---------+--------+------------+
| ename | sal | deptno | dname |
+--------+---------+--------+------------+
| SMITH | 800.00 | 20 | RESEARCH |
| ALLEN | 1600.00 | 30 | SALES |
| WARD | 1250.00 | 30 | SALES |
| JONES | 2975.00 | 20 | RESEARCH |
| MARTIN | 1250.00 | 30 | SALES |
| BLAKE | 2850.00 | 30 | SALES |
| CLARK | 2450.00 | 10 | ACCOUNTING |
| SCOTT | 3000.00 | 20 | RESEARCH |
| KING | 5000.00 | 10 | ACCOUNTING |
| TURNER | 1500.00 | 30 | SALES |
| ADAMS | 1100.00 | 20 | RESEARCH |
| JAMES | 950.00 | 30 | SALES |
| FORD | 3000.00 | 20 | RESEARCH |
| MILLER | 1300.00 | 10 | ACCOUNTING |
+--------+---------+--------+------------+
14 rows in set (0.00 sec)
// 再根据 deptno 排序
mysql> select emp.ename, emp.sal, emp.deptno, dept.dname from emp, dept where emp.deptno = dept.deptno order by deptno;
+--------+---------+--------+------------+
| ename | sal | deptno | dname |
+--------+---------+--------+------------+
| CLARK | 2450.00 | 10 | ACCOUNTING |
| MILLER | 1300.00 | 10 | ACCOUNTING |
| KING | 5000.00 | 10 | ACCOUNTING |
| SMITH | 800.00 | 20 | RESEARCH |
| JONES | 2975.00 | 20 | RESEARCH |
| FORD | 3000.00 | 20 | RESEARCH |
| SCOTT | 3000.00 | 20 | RESEARCH |
| ADAMS | 1100.00 | 20 | RESEARCH |
| TURNER | 1500.00 | 30 | SALES |
| ALLEN | 1600.00 | 30 | SALES |
| MARTIN | 1250.00 | 30 | SALES |
| WARD | 1250.00 | 30 | SALES |
| BLAKE | 2850.00 | 30 | SALES |
| JAMES | 950.00 | 30 | SALES |
+--------+---------+--------+------------+
14 rows in set (0.00 sec)
显示部门号为10的部门名,员工名和工资
- 在第一个查询
select dname, ename, sal from emp, dept where emp.deptno = dept.deptno and emp.deptno = 10;
中,列名dname
、ename
和sal
没有指定表前缀。在这种情况下,MySQL 会按照from子句中表出现的顺序来确定列所属的表。这里它会先在emp表中查找列,找不到就会在dept表中查找。 - 第二个查询
select dept.dname, emp.ename, emp.sal from emp, dept where emp.deptno = dept.deptno and emp.deptno = 10;
明确使用了表前缀(dept.dname、emp.ename和emp.sal)
来指定列所属的表。这种方式更加清晰,尤其是在涉及多个表且列名可能重复的情况下,可以避免歧义。
cpp
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.00 sec)
mysql> select dept.dname, emp.ename, emp.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.01 sec)
显示各个员工的姓名,工资,及工资级别
cpp
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)
mysql> select ename, sal, grade from emp, salgrade where emp.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 |
| CLARK | 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)
- 使用的子查询
cpp
mysql> select empno, ename from emp where empno=(select mgr from emp where ename = 'FORD');
+--------+-------+
| empno | ename |
+--------+-------+
| 007566 | JONES |
+--------+-------+
1 row in set (0.00 sec)
- 使用多表查询(自查询)
cpp
// 使用到表的别名
// from emp leader, emp worker,给自己的表起别名,因为要先做笛卡尔积,所以别名可以先识别
mysql> select leader.empno, leader.ename from emp leader, emp worker where leader.empno = worker.mgr and worker.ename = 'FORD';
+--------+-------+
| empno | ename |
+--------+-------+
| 007566 | JONES |
+--------+-------+
1 row in set (0.00 sec)
🏳️🌈4 子查询
子查询是指嵌入在其他sql语句中的select语句,也叫嵌套查询
❤️4.1 单行子查询
返回一行记录的子查询
显示SMITH同一部门的员工
cpp
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自己
cpp
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)
all关键字;显示工资比部门30的所有员工的工资高的员工的姓名、工资和部门号
cpp
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的任意员工的工资高的员工的姓名、工资和部门号(包含自己部门
的员工)
cpp
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.00 sec)
💛4.3 多列子查询
单行子查询是指子查询只返回单列,单行数据;多行子查询是指返回单列多行数据,都是针对单列而言的,而多列子查询则是指查询返回多个列数据的子查询语句
- 查询和SMITH的部门和岗位完全相同的所有雇员,不含SMITH本人
cpp
mysql> select * from emp where (deptno, job) = (select deptno, job from emp where ename = 'SMITH') andename <> '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子句中。这里要用到数据查询的技巧,把一个子查询当做一个临时表使用。
- 显示每个高于自己部门平均工资的员工的姓名、部门、工资、平均工资
关于临时表中的内容(avg(sal), deptno)的别名问题
像where emp.sal > tmp.avg(sal)这样的写法(假设没有设置别名asal)是错误的。
在外部查询的连接条件emp.deptno = tmp.deptno部分可能会导致语法混淆,最好设置别名
cpp
mysql> select avg(sal) asal, deptno dt from emp group by deptno;
+-------------+------+
| asal | dt |
+-------------+------+
| 2916.666667 | 10 |
| 2175.000000 | 20 |
| 1566.666667 | 30 |
+-------------+------+
3 rows in set (0.00 sec)
// 运用临时表
mysql> select ename, deptno, sal, asal from emp, (select avg(sal) asal, deptno dt from emp group by deptno) tmp where emp.sal > tmp.asal and emp.deptno = tmp.dt;
+-------+--------+---------+-------------+
| ename | deptno | sal | asal |
+-------+--------+---------+-------------+
| KING | 10 | 5000.00 | 2916.666667 |
| JONES | 20 | 2975.00 | 2175.000000 |
| SCOTT | 20 | 3000.00 | 2175.000000 |
| FORD | 20 | 3000.00 | 2175.000000 |
| ALLEN | 30 | 1600.00 | 1566.666667 |
| BLAKE | 30 | 2850.00 | 1566.666667 |
+-------+--------+---------+-------------+
6 rows in set (0.00 sec)
- 查找每个部门工资最高的人的姓名、工资、部门、最高工资
cpp
// 临时表 tmp 内容
mysql> select max(sal) ms, deptno dt from emp group by deptno;
+---------+------+
| ms | dt |
+---------+------+
| 5000.00 | 10 |
| 3000.00 | 20 |
| 2850.00 | 30 |
+---------+------+
3 rows in set (0.00 sec)
// 运用临时表
mysql> select ename, sal, deptno, ms from emp, (select max(sal) ms, deptno dt from emp group by deptno)tmp where deptno = dt and sal = ms;
+-------+---------+--------+---------+
| ename | sal | deptno | ms |
+-------+---------+--------+---------+
| 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)
- 显示每个部门的信息(部门名,编号,地址)和人员数量
cpp
// 方法1:使用多表
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 | 部门人数 |
+------------+--------+----------+--------------+
| ACCOUNTING | 10 | NEW YORK | 3 |
| RESEARCH | 20 | DALLAS | 5 |
| SALES | 30 | CHICAGO | 6 |
+------------+--------+----------+--------------+
3 rows in set (0.00 sec)`V`
// 方法2:使用子查询
mysql> select dept.deptno, dname, mycnt, loc from dept, (select count(*) mycnt, deptno from emp group by deptno) tmp where dept.deptno = tmp.deptno;
+--------+------------+-------+----------+
| deptno | dname | mycnt | loc |
+--------+------------+-------+----------+
| 10 | ACCOUNTING | 3 | NEW YORK |
| 20 | RESEARCH | 5 | DALLAS |
| 30 | SALES | 6 | CHICAGO |
+--------+------------+-------+----------+
3 rows in set (0.00 sec)
💙4.5 合并查询
在实际应用中,为了合并多个select的执行结果,可以使用集合操作符 union,union all
4.5.1 union
该操作符用于取得两个结果集的并集。当使用该操作符时,会自动去掉结果集中的重复行。
- 将工资大于2500或职位是MANAGER的人找出来
cpp
mysql> select ename, sal, job from emp where sal > 2500 union select ename, sal, job from emp where job = 'MANAGER';
+-------+---------+-----------+
| ename | sal | job |
+-------+---------+-----------+
| JONES | 2975.00 | MANAGER |
| BLAKE | 2850.00 | MANAGER |
| SCOTT | 3000.00 | ANALYST |
| KING | 5000.00 | PRESIDENT |
| FORD | 3000.00 | ANALYST |
| CLARK | 2450.00 | MANAGER |
+-------+---------+-----------+
6 rows in set (0.00 sec)
4.5.3 union all
该操作符用于取得两个结果集的并集。当使用该操作符时,不会去掉结果集中的重复行。
- 将工资大于25000或职位是MANAGER的人找出来
cpp
mysql> select ename, sal, job from emp where sal > 200 union all
-> select ename, sal, job from emp where job = 'MANAGER';
+--------+---------+-----------+
| ename | sal | job |
+--------+---------+-----------+
| SMITH | 800.00 | CLERK |
| ALLEN | 1600.00 | SALESMAN |
| WARD | 1250.00 | SALESMAN |
| JONES | 2975.00 | MANAGER |
| MARTIN | 1250.00 | SALESMAN |
| BLAKE | 2850.00 | MANAGER |
| CLARK | 2450.00 | MANAGER |
| SCOTT | 3000.00 | ANALYST |
| KING | 5000.00 | PRESIDENT |
| TURNER | 1500.00 | SALESMAN |
| ADAMS | 1100.00 | CLERK |
| JAMES | 950.00 | CLERK |
| FORD | 3000.00 | ANALYST |
| MILLER | 1300.00 | CLERK |
| JONES | 2975.00 | MANAGER |
| BLAKE | 2850.00 | MANAGER |
| CLARK | 2450.00 | MANAGER |
+--------+---------+-----------+
17 rows in set (0.00 sec)
👥总结
本篇博文对 【MySQL】复合查询 做了一个较为详细的介绍,不知道对你有没有帮助呢
觉得博主写得还不错的三连支持下吧!会继续努力的~