mysql 查询数据库中第n大数据的值
2017-05-04 14:47
225 查看
Write a SQL query to get the nth highest salary from the
For example, given the above Employee table, the nth highest salary where n = 2 is
If there is no nth highest salary, then the query should return
答案:
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
declare m int;
set m=n-1;
RETURN (
# Write your MySQL query statement below.
select distinct Salary from Employee order by salary desc limit m,1
);
END
limit函数 :
一个参数(10):检索前10个数据
两个参数(8,10):检索第8个数据,查询出十个
1、offset比较小的时候。
select * from yanxue8_visit limit 10,10
多次运行,时间保持在0.0004-0.0005之间
Select * From yanxue8_visit Where vid >=(
Select vid From yanxue8_visit Order By vid limit 10,1
) limit 10
多次运行,时间保持在0.0005-0.0006之间,主要是0.0006
结论:偏移offset较小的时候,直接使用limit较优。这个显示是子查询的原因。
2、offset大的时候。
select * from yanxue8_visit limit 10000,10
多次运行,时间保持在0.0187左右
Select * From yanxue8_visit Where vid >=(
Select vid From yanxue8_visit Order By vid limit 10000,1
) limit 10
多次运行,时间保持在0.0061左右,只有前者的1/3。
可以预先offset越大,后者越优。
mysql> SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.
//如果只给定一个参数,它表示返回最大的记录行数目
Employeetable.
+----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+
For example, given the above Employee table, the nth highest salary where n = 2 is
200.
If there is no nth highest salary, then the query should return
null.
答案:
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
declare m int;
set m=n-1;
RETURN (
# Write your MySQL query statement below.
select distinct Salary from Employee order by salary desc limit m,1
);
END
limit函数 :
一个参数(10):检索前10个数据
两个参数(8,10):检索第8个数据,查询出十个
1、offset比较小的时候。
select * from yanxue8_visit limit 10,10
多次运行,时间保持在0.0004-0.0005之间
Select * From yanxue8_visit Where vid >=(
Select vid From yanxue8_visit Order By vid limit 10,1
) limit 10
多次运行,时间保持在0.0005-0.0006之间,主要是0.0006
结论:偏移offset较小的时候,直接使用limit较优。这个显示是子查询的原因。
2、offset大的时候。
select * from yanxue8_visit limit 10000,10
多次运行,时间保持在0.0187左右
Select * From yanxue8_visit Where vid >=(
Select vid From yanxue8_visit Order By vid limit 10000,1
) limit 10
多次运行,时间保持在0.0061左右,只有前者的1/3。
可以预先offset越大,后者越优。
mysql> SELECT * FROM table LIMIT 95,-1; // 检索记录行 96-last.
//如果只给定一个参数,它表示返回最大的记录行数目
相关文章推荐
- 四种数据库(SqlServer/Oracle/Mysql/Sybase)查询前N条数据
- MySQL、MS SQL、Oracle三种数据库查询M到N行数据 分类: 数据库 2009-11-10 23:33 688人阅读 评论(0) 收藏
- mysql 常用命令,连接数据库,查看建表语句,批量导入数据,批量更新数据,连接查询
- MySQL 设置数据按条件查询下的序号 并存入数据库
- mysql 创建数据库 表 插入数据 查询
- MySQL入门(创建数据库以及插入,修改,查询数据)
- mysql 按时间类型【datetime】数据库查询今天,昨天,一周的数据
- 第15天(就业班) 课程回顾、mysql安装、管理数据库、管理表、增删改数据、查询数据
- 【数据库】Mysql数据查询
- mysql 数据库查询的数据有一个字段竟然乱码
- django查询mysql数据 从数据库提出并分页展示
- MySQL中数据库、数据表、字段的字符集查询方式
- mysql查询数据库所有表的数据量
- MySQL、MS SQL、Oracle三种数据库查询M到N行数据
- 如何查询Oracle,Sql Server,MySQL 中的数据库名称、数据表名称、字段名称
- MySQL跨数据库查询数据
- SQL、MySQL、Oracle、 Sqlite、Informix数据库查询指定条数数据的方法
- 【数据库】MySQL乱码解决(存入数据乱码和查询输出乱码)
- MySQL中数据结果集分页功能的实现方法;数据库查询返回特定结果即分页查询
- MySql条件查询不到数据 → 更改数据库编码