数据表:Employee表,字段为id,salary
查询条件:获取Employee表中第n高的薪水,如果不存在,则查询返回null。
本题主要考察的是SQL语言中自定义函数的使用
# 创建函数之前需要制定一个参数 set global log_bin_trust_function_creators=TRUE; # 创建函数 CREATE FUNCTION getNthHighestSalary(n INT) RETURNS INT BEGIN DECLARE s INT; SET s = n-1; RETURN( select IFNULL((select distinct Salary from Employee1 order by Salary desc limit s, 1), null) as getNthHighestSalary ); END; # 调用自定义函数 SELECT getNthHighestSalary(2);
查询结果:
原文:https://www.cnblogs.com/yanmai/p/13045276.html