查询第N高的工资

通过自定义变量来实现。

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
    select IFNULL(Salary, NULL) Salary from (
      select @row_num := @row_num+1 Rank, Salary from (
        select Salary from Employee group by Salary desc
      ) t1 join (
        select @row_num := 0 from dual
      ) t2
    ) t where t.Rank=N
  );
END

通过

你可能感兴趣的:(Mysql)