标签:分享 returns 第一个 info max 分数 creat UNC func
176:第二高的薪水
select (select distinct salary from employee order by salary limit 1 offset 1) as secondhighestsalary; ---去掉第一个,再从第一个开始
177:第N高的薪水
------相关子查询:子查询中引用了外层查询所引用表的字段
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN RETURN ( # Write your MySQL query statement below. select max(salary) from employee e1 where N=(select count(distinct salary) from employee e2 where e2.salary >=e1.salary) ); END
----执行步骤,1.从父查询中取出每一条salary,与子查询中的e2.salary进行比较,直到当大于e2.salary的记录等于n
2.父查询输出最大的salary
https://blog.csdn.net/mascf/article/details/50288199
178. 分数排名
---相关子查询
---方法一
select score,(select count(distinct score) from scores e1 where e1.score>= e2.score) rank from scores e2 order by rank;
标签:分享 returns 第一个 info max 分数 creat UNC func
原文地址:https://www.cnblogs.com/papio/p/9824935.html