16

有人帮我从 MYSQL 的薪水表中找出第 n 高的薪水

4

26 回答 26

44

Try this, n would be the nth item you would want to return

 SELECT DISTINCT(Salary) FROM table ORDER BY Salary DESC LIMIT n,1
于 2012-08-02T07:46:08.977 回答
34

如果您想从表中找到第 n 个薪水(这里的 n 应该是第 1 或第 2 或第 15 最高薪水之类的任何东西)

这是查找第 n 个薪水的查询:

SELECT DISTINCT Salary FROM tblemployee ORDER BY Salary DESC LIMIT 1 OFFSET (n-1)

如果你想找到第 8 高的薪水,查询应该是:

SELECT DISTINCT Salary FROM tblemployee ORDER BY Salary DESC LIMIT 1 OFFSET 7

注意: OFFSET 从第 0 个位置开始,因此这里使用 N-1 规则

于 2015-02-04T10:15:01.460 回答
18

要获得第 n 高薪水,您需要先使用排序数据ORDER BY,然后使用LIMITwith选择第 n 高记录OFFSET

SELECT DISTINCT(salary) AS salary
FROM tbl_salary
ORDER BY salary DESC
LIMIT 1 OFFSET (n - 1);
于 2012-08-02T06:19:13.597 回答
12
SELECT * FROM Employee Emp1 
WHERE (N-1) = ( 
    SELECT COUNT(DISTINCT(Emp2.Salary)) 
    FROM  Employee Emp2 
    WHERE Emp2.Salary > Emp1.Salary)

对于外部查询处理的每条记录,都会执行内部查询,并返回有多少条记录的工资低于当前工资。如果您正在寻找第二高的薪水,那么一旦内部查询返回 N-1,您的查询就会停止。

于 2013-03-20T09:54:37.910 回答
8

寻找最高薪水

select MAX(Salary) from Employee;

找到第二高的薪水

查询-1

SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee);

查询-2

select MAX(Salary) from Employee
WHERE Salary <> (select MAX(Salary) from Employee )

找到第 n 个最高薪水

查询-1

SELECT * /*This is the outer query part */
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)

查询-2

SELECT *
FROM Employee Emp1
WHERE (1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)

在 SQL Server 中使用 TOP 关键字排名第 n 高的薪水

SELECT TOP 1 Salary
FROM (
      SELECT DISTINCT TOP N Salary
      FROM Employee
      ORDER BY Salary DESC
      ) AS Emp
ORDER BY Salary

在 MySQL 中查找第 n 个最高薪水

SELECT Salary FROM Employee 
ORDER BY Salary DESC LIMIT n-1,1

在 SQL Server 中查找第 n 个最高薪水

SELECT Salary FROM Employee 
ORDER BY Salary DESC OFFSET N-1 ROW(S) 
FETCH FIRST ROW ONLY

使用 rownum 在 Oracle 中查找第 n 个最高薪水

select * from (
  select Emp.*, 
row_number() over (order by Salary DESC) rownumb 
from Employee Emp
)
where rownumb = n;  /*n is nth highest salary*/

使用 RANK 查找 Oracle 中第 n 高的薪水

select * FROM (
select EmployeeID, Salary
,rank() over (order by Salary DESC) ranking
from Employee
)
WHERE ranking = N;
于 2015-11-27T11:27:50.873 回答
4

在这里,我们可以为此创建 MYSQL 函数。Employee 表中的第 n 个最高薪水。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

例如,给定上面的 Employee 表,n = 2 的第 n 个最高薪水是 200。如果没有第 n 个最高薪水,那么查询应该返回 null。

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE limitv INT;
    SET limitv = N - 1;
  RETURN (
      Select IFNULL(
        (select Distinct Salary from Employee order by Salary Desc limit limitv, 1),
         NULL
      ) as getNthHighestSalary
  );
END
于 2019-10-14T10:13:46.587 回答
3

尝试这个:

select MIN(sal) from salary where sal in 
 (select sal from salary order by sal desc limit 9)
于 2012-08-02T06:25:50.270 回答
2

如果想指定第 n 高,可以使用排名方法。

要获得第三高,请使用

SELECT * FROM
(SELECT @rank := @rank + 1 AS rank, salary
FROM   tbl,(SELECT @rank := 0) r 
order by salary desc ) m
WHERE rank=3
于 2012-08-02T06:23:12.730 回答
2

试试这个来找到第五高的薪水-

SELECT DISTINCT(column name) FROM table ORDER BY (column name) desc LIMIT 4,1

第n个薪水-

SELECT DISTINCT(column name) FROM table ORDER BY (column name) desc LIMIT n-1,1

我在phpmyadmin面板上试过了。。

于 2014-05-20T12:08:30.810 回答
2
+-------+--------+
|  name | salary |
+-------+--------+
|   A   | 100    |
|   B   | 200    |
|   C   | 300    |
|   D   | 400    |
|   E   | 500    |
|   F   | 500    |
|   G   | 600    |
+-------+--------+

如果您只想选择第 N 个最高薪水,那么:

SELECT DISTINCT salary FROM emp ORDER BY salary DESC LIMIT 1 OFFSET N-1;

如果您想选择所有获得第 N 高薪水的员工,那么:

SELECT * FROM emp WHERE salary = (
   SELECT DISTINCT salary FROM emp ORDER BY salary DESC LIMIT 1 OFFSET N-1
);
于 2020-03-14T12:26:17.313 回答
1
SELECT * FROM employe e1 WHERE n-1 = ( SELECT COUNT(DISTINCT(e2.salary)) FROM employe e2 WHERE e2.salary > e1.salary) 

    Where n = highest number of salary like 1,2,3
于 2014-09-06T09:33:21.897 回答
1

首先对所有记录进行排序,确实会消耗大量时间(想象一下,如果表包含数百万条记录)。然而,诀窍是进行改进的线性搜索。

SELECT * FROM Employee Emp1
WHERE (N-1) = ( SELECT COUNT(*) FROM (
    SELECT DISTINCT(Emp2.Salary)
    FROM  Employee Emp2
    WHERE Emp2.Salary > Emp1.Salary LIMIT N))

在这里,一旦内部查询发现 n 个不同的薪水值大于外部查询的薪水,它就会将结果返回给外部查询。

Mysql 在http://dev.mysql.com/doc/refman/5.6/en/limit-optimization.html已经清楚地提到了这个优化

上面的查询也可以写成,

SELECT * FROM Employee Emp1
WHERE (N-1) = (
    SELECT COUNT(DISTINCT(Emp2.Salary))
    FROM  Employee Emp2
    WHERE Emp2.Salary > Emp1.Salary LIMIT N)

同样,如果查询与仅在单个表上运行一样简单并且仅用于提供信息的目的,那么您可以通过将第 n 个最高薪水放在 where 子句中来限制最外层查询以返回 1 条记录并运行单独的查询

感谢 Abishek Kulkarni 的解决方案,建议对此进行优化。

于 2015-12-14T15:33:41.057 回答
1
select distinct(column_name) from table_name order by column_name desc limit (n-1),1;
于 2018-05-15T06:36:42.367 回答
1

MySQL 查询从工资表中查找第 N 个最高工资(100% 正确)

SELECT Salary from Employee ORDER BY Salary DESC LIMIT N-1,1;

于 2018-06-16T15:13:50.560 回答
0
SELECT DISTINCT(column_name)
  FROM table_name 
  ORDER BY column_name DESC limit N-1,1;

其中 N 代表第 n 个最高薪水..

第三高薪:

SELECT DISTINCT(column_name)
 FROM table_name 
 ORDER BY column_name DESC limit 2,1;
于 2013-02-11T22:37:12.120 回答
0

第四高薪水:

select min(salary) from (select distinct Salary from hibernatepractice.employee e order by Salary desc limit 4) as e1;

对于第 n 个最高工资:

select min(salary) from (select distinct Salary from hibernatepractice.employee e order by Salary desc limit n) as e1;

于 2016-03-03T12:37:59.220 回答
0

获得第二高的薪水:

SELECT   salary 
FROM     [employees] 
ORDER BY salary DESC 
offset   1 rows 
FETCH next 1 rows only

要获得第 N 高薪水:

SELECT   salary 
FROM     [employees] 
ORDER BY salary DESC 
offset   **n-1** rows 
FETCH next 1 rows only
于 2017-09-27T10:12:40.530 回答
0

试试这个解决方案。

select SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT salary ORDER BY salary DESC),',',3),',',-1) from employees
于 2018-09-17T07:05:26.277 回答
0
set @cnt=0;
select s.* from (SELECT  (@cnt := @cnt + 1) AS rank,a.* FROM one as a  order by a.salary DESC) as s WHERE s.rank='3';

=>这是第三高的薪水。第 n 次替换 3 值。例如第五高:

set @cnt=0;
select s.* from (SELECT  (@cnt := @cnt + 1) AS rank,a.* FROM one as a  order by a.salary DESC) as s WHERE s.rank='5';
于 2018-02-21T18:22:28.257 回答
0

这是工资表

在此处输入图像描述

SELECT  amount FROM  salary 
GROUP by amount
ORDER BY  amount DESC 
LIMIT n-1 , 1

或者

SELECT DISTINCT amount
FROM  salary 
ORDER BY  amount DESC 
LIMIT n-1 , 1
于 2016-12-06T16:31:56.247 回答
0

我已经为此查询使用了过程

这里getHighestSalary的程序,报告 Employee 表中的第 n 个最高薪水。如果没有第 n 个最高薪水,则查询应报告 null。

首先,创建表

CREATE TABLE employee (
id INT AUTO_INCREMENT,
salary INT,
PRIMARY KEY (id) );

接下来,创建 PROCEDURE

DELIMITER // 
 CREATE PROCEDURE getHighestSalary(emp_id int)
 BEGIN
    select ifnull((select salary from employee where id = emp_id order by salary desc), null) as getNthHighestSalary;
 END //
 DELIMITER ;

最后,调用具有 n 值的 getHighestSalary 过程

call getHighestSalary(2); -- 200
于 2022-01-21T06:59:15.060 回答
0
SET @cnt=0; 
SELECT s.* 
FROM   (SELECT ( @cnt := @cnt + 1 ) AS rank, 
               a.* 
        FROM   one AS a 
        ORDER  BY a.salary DESC) AS s 
WHERE  s.rank = '3'; 
于 2018-02-21T18:17:17.620 回答
0

让表格salaries包含

+----------+--------+--------+
| emp      | salary | deptno |
+----------+--------+--------+
| ep1      |     10 | dp1    |
| ep2      |     20 | dp2    |
| ep3      |     30 | dp2    |
| ep4      |     40 | dp1    |
| ep5      |     50 | dp1    |
| ep6      |     60 | dp3    |
| ep7      |     70 | dp3    |
+----------+--------+--------+

通过嵌套查询:(您可以在其中更改偏移量 0/1/2... 分别为第一、第二和第三...位置)

 select
      *
    from
      salaries as t1 
    where 
      t1.salary = (select 
                   salary
                 from 
                   salaries
                 where 
                 salaries.deptno = t1.deptno ORDER by salary desc limit 1 offset 1);

或者可能是通过创建排名:(您可以在其中更改排名= 1/2/3 ...分别为第​​一,第二和第三...位置)

SET @prev_value = NULL;
SET @rank_count = 0;
select * from 
(SELECT
  s.*, 
  CASE 
      WHEN @prev_value = deptno THEN @rank_count := @rank_count + 1
      WHEN @prev_value := deptno THEN @rank_count := 1 
      ELSE @rank_count := 1 
  END as rank
FROM salaries s
ORDER BY deptno, salary desc) as t
having t.rank = 2;
于 2018-10-24T17:50:28.610 回答
0

获取第 n 高记录的查询如下:

SELECT 
    *
FROM
    (SELECT 
        *
    FROM
        table_name
    ORDER BY column_name ASC
    LIMIT N) AS tbl
ORDER BY column_name DESC
LIMIT 1;

简单易懂

于 2016-01-19T06:54:16.037 回答
0

这将有效找到第n 个最大数

SELECT 
    TOP 1 * from (SELECT TOP  nth_largest_no * FROM Products Order by price desc) ORDER BY price asc;

对于第五大数字

SELECT 
  TOP 1 *  from (SELECT TOP  5 * FROM Products Order by price desc) ORDER BY price asc;
于 2016-03-08T10:06:11.893 回答
-1

如果您想获取薪水第三高的员工的所有记录,则可以使用以下sql查询:

表名:工资

select * from salary where salary = 
(select distinct salary from salary order by salary desc limit 2,1)
于 2014-06-17T11:52:49.570 回答