以下面的题目为例子简单的总结 一下MySQL语句使用:
The Employee
table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.
create table Employee(Id int unsigned not null auto_increment primary key,Name char(8) not null,Salary char(4) not null,DepartmentId int);
insert into Employee(Id, Name, Salary, DepartmentId) values(’Joe’,’70000’,’1’);
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
+----+-------+--------+--------------+
The Department
table holds all departments of the company.
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
在MySQL中构建数据表:
mysql> usemysql;
mysql> create table Employee(Id int unsigned not null auto_increment primary key,Name varchar(20) not null,Salary varchar(20) not null,DepartmentId int);
mysql> insert into Employee values(1,'Joe','70000',1);
Query OK, 1 row affected (0.04 sec)
mysql> insert into Employee values(2,'Henry','80000',2),(3,'Sam','60000',2),(4,'Max','90000',1);
mysql> select * from employee;
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
+----+-------+--------+--------------+
4 rows in set (0.00 sec)
mysql> create table Department(Id int primary key not Null,Name varchar(20));
Query OK, 0 rows affected (0.55 sec)
mysql>
mysql> insert into Department values(1,'IT'),(2,'Sales');
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from Department;
+----+-------+
| Id | Name |
+----+-------+
| 1 | IT |
| 2 | Sales |
+----+-------+
2 rows in set (0.00 sec)
内连接Employee和Department表:
mysql> select * from Employee as e inner join Department as d on e.DepartmentId=d.Id;
+----+-------+--------+--------------+----+-------+
| Id | Name | Salary | DepartmentId | Id | Name |
+----+-------+--------+--------------+----+-------+
| 1 | Joe | 70000 | 1 | 1 | IT |
| 2 | Henry | 80000 | 2 | 2 | Sales |
| 3 | Sam | 60000 | 2 | 2 | Sales |
| 4 | Max | 90000 | 1 | 1 | IT |
+----+-------+--------+--------------+----+-------+
4 rows in set (0.00 sec)
根据部门排序,获取每个部门的最高薪水表:
mysql> select e.DepartmentId, MAX(e.Salary) as Salary, d.Name as Department from Employee as e inner join Department as d
on e.DepartmentId = d.Id group by e.DepartmentId;
+--------------+--------+------------+
| DepartmentId | Salary | Department |
+--------------+--------+------------+
| 1 | 90000 | IT |
| 2 | 80000 | Sales |
+--------------+--------+------------+
2 rows in set (0.00 sec)
使用上面的部门最高薪资表跟Employee内连接,获取最终的信息:
mysql> select t.Name as Department,e.Name as Employee, e.Salary as Salary from Employee as e inner join (select e.DepartmentId,max(e.Salary) as Salary,d.Name from Employee as e inner join Department as d on e.DepartmentId =d.Id group by e.DepartmentId) as t on e.DepartmentId=t.DepartmentId and e.Salary=t.Salary;
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| Sales | Henry | 80000 |
| IT | Max | 90000 |
+------------+----------+--------+
2 rows in set (0.00 sec)