首先给出本文所需的表格:
emp表:
mysql> select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 880.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1760.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1375.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.00 sec)
dept表:
mysql> select * from dept;
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
4 rows in set (0.00 sec)
方法一:建立两个左连接
#第一步:获得每个部门的最高薪水
mysql>
select
deptno,max(sal) maxsal
from
emp
group by
deptno;
+--------+---------+
| deptno | maxsal |
+--------+---------+
| 20 | 3000.00 |
| 30 | 2850.00 |
| 10 | 5000.00 |
+--------+---------+
#第二步建立两个左连接表
mysql>
select
d.dname,t.deptno,e.ename,t.maxsal
from
emp e
left join
(select deptno,max(sal) maxsal from emp group by deptno) t
on
t.deptno = e.deptno and t.maxsal=e.sal
left join
dept as d
on
e.deptno = d.deptno;
+------------+--------+-------+---------+
| dname | deptno | ename | maxsal |
+------------+--------+-------+---------+
| RESEARCH | 20 | SCOTT | 3000.00 |
| RESEARCH | 20 | FORD | 3000.00 |
| SALES | 30 | BLAKE | 2850.00 |
| ACCOUNTING | 10 | KING | 5000.00 |
+------------+--------+-------+---------+
4 rows in set (0.00 sec)
法二:建立一个左连接
#第一步找出每个部门最高的薪水值
mysql>
select d
eptno ,max(sal)
from
emp
group by
deptno;
+--------+----------+
| deptno | max(sal) |
+--------+----------+
| 20 | 3000.00 |
| 30 | 2850.00 |
| 10 | 5000.00 |
+--------+----------+
3 rows in set (0.00 sec)
#第二步:上面的表为临时表,将emp表与dept表之间建立一个左连接,条件是emp表中的部门和薪水等于临时表中部门和薪水
mysql>
select
d.dname,e.deptno,e.ename,e.sal
from
emp as e join dept as d
on
e.deptno = d.deptno
where
(e.deptno,e.sal) IN (select deptno ,max(sal) from emp group by deptno);
+------------+--------+-------+---------+
| dname | deptno | ename | sal |
+------------+--------+-------+---------+
| SALES | 30 | BLAKE | 2850.00 |
| RESEARCH | 20 | SCOTT | 3000.00 |
| ACCOUNTING | 10 | KING | 5000.00 |
| RESEARCH | 20 | FORD | 3000.00 |
+------------+--------+-------+---------+
4 rows in set (0.00 sec)