https://leetcode.com/problems/second-highest-salary/description/
题目要求输出第二大的成绩。
首先可以想到去重,然后排序,相当于输出第二个成绩。
这个用limit a offset b表示,从第b项开始,选出a个,下标从0开始
然后,如果不存在,那么要输出null,需要用isnull判断。
isnull(a, b)表示如果a是空,那么输出b,否则输出a
# Write your MySQL query statement below select IFNULL((select distinct Salary from Employee order by Salary desc limit 1 offset 1), null) as SecondHighestSalary;
这个,用一个子查询可以不用isnull判断的
# Write your MySQL query statement below select ((select distinct Salary from Employee order by Salary desc limit 1 offset 1)) as SecondHighestSalary;
这个错的
SELECT DISTINCT Salary AS SecondHighestSalary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1
其实还不太懂,两者的区别,后者是因为没有第二大,所以可能取不到。然后放在子查询会OK了?