当前位置: 移动技术网 > IT编程>数据库>Mysql > LeetCode 176. 第二高的薪水(MySQL版)

LeetCode 176. 第二高的薪水(MySQL版)

2019年03月24日  | 移动技术网IT编程  | 我要评论

0.前言

  最近刷leetcode 刷数据库题目 由于数据库课上的是sql,而mysql有许多自己的函数的,怕把刚学会的函数忘记 特在此记录!

1.题目

  

编写一个 sql 查询,获取 employee 表中第二高的薪水(salary) 。

+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
例如上述 employee 表,sql查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。

+---------------------+
| secondhighestsalary |
+---------------------+
| 200                 |
+---------------------+

  

2.用到的知识点

  

limit : limit可以接收两个参数   limit 10 或者 limit 0,10

   前者表示查询显示前10行

  后者表示从第0行的往后10行,也就是第1行到第10行

 

ifnull(expression_1,expression_2);

  如果expression_1不为null 就显示自己,否则显示expression_2

  类似java expression1 != null? expression1:expression2

3.最终的sql语句 

select ifnull
(

(select distinct salary from employee  order by salary desc limit 1,1),(null)

) as secondhighestsalary

  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网