Mysql SQL-第二高
我试图解决下面的leet代码问题 这个答案怎么了?以下答案不被接受:Mysql SQL-第二高,mysql,sql,Mysql,Sql,我试图解决下面的leet代码问题 这个答案怎么了?以下答案不被接受: select t.salary as SecondHighestSalary from ( select salary from employee order by salary desc limit 1 offset 1 ) as t 这是可以接受的 select salary as SecondHighestSalary from employee a where 1 = (select
select t.salary as SecondHighestSalary from
(
select salary
from employee
order by salary desc
limit 1 offset 1
) as t
这是可以接受的
select salary as SecondHighestSalary
from employee a
where 1 = (select count(1) from employee b where b.salary < a.salary )
如果最低工资在表中出现两次,您的查询将不起作用
上面的查询使用了相关子查询,也就是说,对于外部表中的每一行,子查询或内部查询将执行一次
此外,这将是第n个最高工资。如果要求的是第十高的薪水,则只需将上述查询中的1替换为9。以下两个答案均被接受 我们可以使用如下所示的子查询:
SELECT MAX(salary) AS secondhighestsalary
FROM employee
WHERE salary < (SELECT MAX(salary)
FROM employee);
或者,您也可以使用临时表:
with temp as
(
SELECT MAX(salary) as salary FROM employee
)
select max(salary) as secondhighestsalary from employee where salary <(select salary from temp);
错误:根据薪资描述限制1,1;,从员工订单中选择不同的薪资次高薪资?两名员工可能有相同的薪水。@GordonLinoff-这是一个很好的观点。顺便说一句,这是一个相关的子查询,我忍不住觉得这让问题变得过于复杂了