查找MySQL Employee表的最高和第二最高薪水?
您可以使用LIMITOFFSET从Employee表中获得最高和第二最高工资。语法如下-
SELECT yourColumnName1,yourColumnName2,....N from yourTableName ORDER BY yourColumnName desc limit 2 offset 0;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table EmployeeMaxAndSecondMaxSalary -> ( -> EmployeeId int, -> Employeename varchar(20), -> EmployeeSalary int -> );
使用插入命令在表中插入一些记录-
mysql> insert into EmployeeMaxAndSecondMaxSalary values(1,'John',34566); mysql> insert into EmployeeMaxAndSecondMaxSalary values(2,'Bob',56789); mysql> insert into EmployeeMaxAndSecondMaxSalary values(3,'Carol',44560); mysql> insert into EmployeeMaxAndSecondMaxSalary values(4,'Sam',76456); mysql> insert into EmployeeMaxAndSecondMaxSalary values(5,'Mike',65566); mysql> insert into EmployeeMaxAndSecondMaxSalary values(6,'David',89990); mysql> insert into EmployeeMaxAndSecondMaxSalary values(7,'James',68789); mysql> insert into EmployeeMaxAndSecondMaxSalary values(8,'Robert',76543);
使用select语句显示表中的所有记录。查询如下-
mysql> select *from EmployeeMaxAndSecondMaxSalary;
以下是输出-
+------------+--------------+----------------+ | EmployeeId | Employeename | EmployeeSalary | +------------+--------------+----------------+ | 1 | John | 34566 | | 2 | Bob | 56789 | | 3 | Carol | 44560 | | 4 | Sam | 76456 | | 5 | Mike | 65566 | | 6 | David | 89990 | | 7 | James | 68789 | | 8 | Robert | 76543 | +------------+--------------+----------------+ 8 rows in set (0.00 sec)
以下是使用限制偏移量获取最高和第二最高工资的查询-
mysql> select EmployeeId,Employeename,EmployeeSalary from EmployeeMaxAndSecondMaxSalary order by -> EmployeeSalary desc limit 2 offset 0;
输出显示2名雇员的最高薪水记录-
+------------+--------------+----------------+ | EmployeeId | Employeename | EmployeeSalary | +------------+--------------+----------------+ | 6 | David | 89990 | | 8 | Robert | 76543 | +------------+--------------+----------------+ 2 rows in set (0.00 sec)