簡體   English   中英

我想在表格中找到所有關於第二高薪水的記錄,並且有許多薪水第二高的員工如何做到這一點

[英]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

我想找到表中薪水第二高的所有記錄。 有很多這樣的員工,我該怎么做?

表:員工

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

您可以按如下方式使用dense_rank

Select * from
(Select t.*,
       dense_rank() over (order by salary desc) rn
  From your_table t) t
Where rn = 2

如果您使用的是 mysql 的較低版本,其中 windows 函數是不允許的,那么您可以使用如下關聯查詢:

Select t.*
  From your_table t
 Where 1 = (select count(distinct tt.salary)
             From your_table tt
            Where tt.salary > t.salary)

您可能會發現這樣做很簡單:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM