Mysql检索第n条记录

时间:2013-11-11 10:51:03

标签: mysql sql

假设我有一个名为EMPLOYEE的表,其中包含以下属性

(EMPLOYEE_ID, LAST_NAME, FIRST_NAME, MIDDLE_NAME, JOB_ID, MANAGER_ID, Salary)

我能吗? 显示第N个最高工资图纸员工详细信息 请帮忙

3 个答案:

答案 0 :(得分:4)

ORDER BYLIMIT其中10n + 1

SELECT
    *
FROM
    employees
ORDER BY
    Salary DESC
LIMIT
    10, 1

(如果你想要第一条记录,请使用LIMIT 0, 1。对于第十条记录,请使用LIMIT 9, 1等。)

答案 1 :(得分:2)

try this

put n > 1 to get corresponding results

n = 3必须给你第二个highest salary

 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 :(得分:1)

测试表

CREATE TABLE Test 
(ID INT IDENTITY(1,1),
Salary INT)

INSERT INTO Test
VALUES (100), (200), (300), (400), (500)

SELECT * FROM Test

查询

SELECT TOP 1 Salary
FROM 
    (SELECT TOP 3 Salary FROM Test ORDER BY Salary DESC)q
ORDER BY Salary ASC

在您的子查询SELECT TOP Nth中,其余部分保持不变,它将为您提供所需的结果