如何获取 MySQL 表格中包含员工薪酬记录的前 3 个薪酬?
为此,使用 LIMIT 和 OFFSET。我们首先创建一个表 -
mysql> create table DemoTable867(EmployeeSalary int); Query OK, 0 rows affected (0.64 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable867 values(63737); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable867 values(899833); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable867 values(23644); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable867 values(89393); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable867 values(534333); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable867 values(889322); Query OK, 1 row affected (0.08 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable867;
这将产生以下输出 -
+----------------+ | EmployeeSalary | +----------------+ | 63737 | | 899833 | | 23644 | | 89393 | | 534333 | | 889322 | +----------------+ 6 rows in set (0.00 sec)
这是获取第一个最高薪酬的查询 -
mysql> select distinct(EmployeeSalary) from DemoTable867 order by EmployeeSalary DESC LIMIT 1;
这将产生以下输出 -
+----------------+ | EmployeeSalary | +----------------+ | 899833 | +----------------+ 1 row in set (0.02 sec)
这是获取第二个最高薪酬的查询 -
mysql> select distinct(EmployeeSalary) from DemoTable867 order by EmployeeSalary DESC LIMIT 1 OFFSET 1;
这将产生以下输出 -
+----------------+ | EmployeeSalary | +----------------+ | 889322 | +----------------+ 1 row in set (0.00 sec)
以下是对第三个最高薪酬进行查询
mysql> select distinct(EmployeeSalary) from DemoTable867 order by EmployeeSalary DESC LIMIT 1 OFFSET 2;
这将产生以下输出 -
+----------------+ | EmployeeSalary | +----------------+ | 534333 | +----------------+ 1 row in set (0.00 sec)
广告