当前位置:网站首页>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
边栏推荐
- 2020-09-17 gateway业务流程 两个任务:referer认证和非商品模板化
- 阿里云服务器安装配置redis,无法远程访问
- UE4 material UV texture does not stretch with model scale
- The principle of session and cookie
- 我想要股票开户优惠,怎么得到?还有,在线开户安全么?
- JS obtain mobile phone model and system version
- 通用分页框架
- kdevelop新建工程
- Data warehouse: layered architecture of Finance / banking
- 你知道BFD是什么吗?一文详解BFD协议原理及使用场景
猜你喜欢

kdevelop新建工程

1.4 regression of machine learning methods

Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer

CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION

2020-09-29 非商品模板化代码层次 rapidjson库

SPI drive of lsm6dsl

ORA-01950 对表空间无权限

装饰器模式的应用,包装ServletRequest,增加addParameter方法

Mysql配置主从数据库

数据可视化:数据可视化四象限,教你正确应用图标
随机推荐
Implementation of multi key state machine based on STM32 standard library
JS obtain mobile phone model and system version
2020-09-21 referer字符串切分 boost gateway代码组织层次
Go deep into RC, RS, daemonset and statefulset (VII)
Wechat applet rewrites the page function to realize global logging
数据治理:元数据管理(第二篇)
基于stm32标准库独立按键的多按键状态机的实现
Segmentation of Head and Neck Tumours Using Modified U-net
gcc与makefile
[technology development] development and design of alcohol tester solution
Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images
Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
微信小程序重写Page函数,实现全局日志记录
转载 :判断对象是否具有属性的5种方法
UE4 compile a single file (VS and editor start respectively)
Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks
Basic operations of MAC MySQL database
Data visualization: the four quadrants of data visualization teach you to correctly apply icons
A comparison of methods for fully automatic segmentation of tumors and involved nodes in PET/CT of h
滑块验证代码