当前位置:网站首页>leetcode MYSQL数据库题目177
leetcode MYSQL数据库题目177
2022-06-29 09:10:00 【水似冰】
177. Nth Highest Salary
1、题目与答案
Table:Employee
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
Write a SQL query to get the nth highest salary from the Employee table.
编写SQL从Employee表中获取第n个最高薪水
答案:delimiter $$ create function getNthHighestSalary(N int) returns int begin declare n1 int; set n1=N-1; return ( select distinct Salary from Employee order by Salary desc limit n1,1 ); end $$
2、知识点总结
- delimiter
- create function
- declare、set
1、之所以使用delimiter是因为MySQL中默认是使用分号来结束一个命令的,我们定义的函数体中一条命令写完时会用分号来结束,而MySQL会误以为函数体已经定义完成,在运行是会报错。为此我们需要定义一个新的标识符来标识一个命令的结束。
2、
创建新函数:
Create function function_name(parameter_type)
returns return_type
begin
#Function_content
end
相关说明:
函数名:应该合法的标识符,并且不应该与已有的关键字冲突。一个函数应该属于某数据库,可以使用db_name.funciton_name的形式执行当前函数所属数据库,否则默认为当前数据库。
参数列表:可以有一个或者多个函数参数,甚至是没有参数也是可以的。对于每个参数,由参数名和参数类型组成。
返回值:指明返回值类类型
函数体:自定义函数的函数体由多条可用的MySQL语句,流程控制,变量声明等语句构成。需要指明的是函数体中一定要含有return 返回语句。
3、
mysql存储过程中,定义变量有两种方式:
- 使用set或select直接赋值,变量名以 @ 开头.
例如:set @var=1;
可以在一个会话的任何地方声明,作用域是整个会话,称为会话变量。 - 以 declare 关键字声明的变量,只能在存储过程中使用,称为存储过程变量,
例如:
DECLARE var1 INT DEFAULT 0; 主要用在存储过程中,或者是给存储传参数中。
补充
LeetCode 上的答案:
select IFNULL((select distinct(Salary)
from Employee
order by Salary desc
limit 1,1),null) as SecondHighestSalary
边栏推荐
- Visual assist plug-in settings for UE4 vs
- c语言printf大家族系列
- Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer
- Could not open JDBC connection for transaction
- 1424. diagonal traversal II
- 2020-09-21 referer字符串切分 boost gateway代码组织层次
- Kicad learning notes - shortcut keys
- Idea debugging fails, reporting jdwp no transports initialized, jvmtierror=agent_ ERROR_ TRANSPORT_ LOAD(196)
- Invalidconnectionattributeexception exception exception handling
- linux下centos7中mysql5.7安装教程
猜你喜欢

In the future of Business Intelligence BI, how do you view the ai+bi model?

zabbix4.4配置监控服务器指标,以及图形页乱码解决

Closed training (25) basic web security

Matlab tips (21) matrix analysis -- partial least squares regression

如何将谷歌浏览器设置为默认浏览器

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

你知道BFD是什么吗?一文详解BFD协议原理及使用场景

IPC(进程间通信)之管道详解

Simplicity studio does not recognize the new JLINK V9 solution

ORA-01950 对表空间无权限
随机推荐
Reading notes on how to connect the network - Web server request and response (V)
c#判断数组是否包含另一个数组的任何项
2020-9-14 广告系统入门
Kicad learning notes - shortcut keys
Data governance: the solution of data governance in the data Arena
数据可视化:数据可视化四象限,教你正确应用图标
Basic operations of MAC MySQL database
Construction and use of Changan chain go language smart contract environment
Data warehouse: layered architecture of Finance / banking
CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
Slider validation code
Install and configure redis in the Linux environment, and set the boot auto start
FreeRTOS(八)——时间管理
Cloud management platform: openstack architecture design and detailed interpretation
linux环境下安装配置redis,并设置开机自启动
云管理平台:OpenStack架构设计及详细解读
Closed training (25) basic web security
Self cultivation (XXI) servlet life cycle, service method source code analysis, thread safety issues
KiCad学习笔记--快捷键
Wechat applet rewrites the page function to realize global logging