如何在不使用限制的情况下在MySql中获得前5名工资

时间:2015-10-21 06:42:34

标签: mysql

以下查询在MySql中无效。

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)

帮助我。

1 个答案:

答案 0 :(得分:5)

试试这个:

SET @n := 0;
SELECT * FROM
(SELECT salary, @n := @n + 1 AS t1 FROM Employee ORDER BY salary DESC)
  AS t
WHERE t1 <= 5

<强> Working DEMO