2016-08-23 54 views

回答

1

您可以通过工资订购并获得唯一进入前3项这将是最高的,位列第二和第三高的薪水一样

select * 
from dept 
order by salary desc 
limit 3; 
+0

如何让无极限 – sms

+0

@sms数据,那么你将不得不使用子查询 – Rahul

1

您可以使用以下SQL来获取数据。

select * 
from dept 
where salary < max(salary) 
order by salary desc 
limit 2; 
+0

是可以无限制 – sms

+0

你不能用'最大()''那样'在'where'条件下的函数。这是唯一可能在'有'条款 – Rahul

+0

哦谢谢你:)。 –

2

试试这个它很简单:

SELECT id, dept_name, DISTINCT salary FROM dept_table DESC salary LIMIT 2 OFFSET 2 
+0

如何获取没有限制是否有可能 – sms

+0

奇怪的是,看到upvotes,有这么多的错误在这个查询中......你不能使用不同的特定列;使用'desc'而不用'order by'。检查文档http://dev.mysql.com/doc/refman/5.7/en/select.html – Rahul

1

你可以不用使用临时数列极限。

SELECT 
    (@cnt := @cnt + 1) AS rowNumber, * 
FROM dept AS t 
    CROSS JOIN (SELECT @cnt := 0) AS dummy 
ORDER BY t.salary DESC ; 

你可以参考这个example

+0

这是一个更好的答案。 – Rahul

0

您可以使用子查询,以及使用TOP关键字。 使用前

SELECT TOP 2 salary 
FROM 
    (SELECT TOP 3 salary 
    FROM Table_Name 
    ORDER BY salary DESC) AS a 
ORDER BY salary ASC 
相关问题