[英]i want to find all the record against second highest salary in table and there many employee with second highest salary how to do that
I want to find all the records with the second highest salary in the table.我想找到表中薪水第二高的所有记录。 There are many such employees, how do I do that?
有很多这样的员工,我该怎么做?
Table: Employee表:员工
ID salary emp_name emp_address
1 400 A abc
2 800 B def
3 300 C hjs
4 400 D teuu
5 400 E kakn
6 400 E kssj
You can use the dense_rank
as follows:您可以按如下方式使用
dense_rank
:
Select * from
(Select t.*,
dense_rank() over (order by salary desc) rn
From your_table t) t
Where rn = 2
If you are on lower version of mysql in which windows functions are not allowed then you can use corelated query as follows:如果您使用的是 mysql 的较低版本,其中 windows 函数是不允许的,那么您可以使用如下关联查询:
Select t.*
From your_table t
Where 1 = (select count(distinct tt.salary)
From your_table tt
Where tt.salary > t.salary)
You might find it simple enough to do:您可能会发现这样做很简单:
select t.*
from t
where t.salary = (select distinct t2.salary
from t t2
order by t2.salary desc
limit 1 offset 1
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.