Db2 例子
create table mynumber(id int,name varchar(10))
insert into mynumber values(1,'no1')
insert into mynumber values(2,'no2')
insert into mynumber values(3,'no3')
insert into mynumber values(4,'no4')
insert into mynumber values(5,'no5')
insert into mynumber values(5,'no6')
insert into mynumber values(6,'no7')
insert into mynumber values(7,'no8')
insert into mynumber values(8,'no9')
insert into mynumber values(9,'no10')
insert into mynumber values(9,'no11')
insert into mynumber values(9,'no12')
insert into mynumber values(10,'no13')
insert into mynumber values(10,'no14')
insert into mynumber values(10,'no15')
insert into mynumber values(11,'no16')
insert into mynumber values(12,'no17')
insert into mynumber values(13,'no18')
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) as tt where case1<=10
case 1: 取前10条不同id记录,如果最后1条记录的ID 还有相同的,都要取出来。
select * from mynumber where id in(select distinct id from mynumber fetch first 10 rows only)
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) as tt where case1<=10
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) as tt where case1 between 5 and 10
case 2:取前10条记录,如果第10条记录的ID 还有相同的,都要取出来。
select * from mynumber where id in(select id from mynumber fetch first 10 rows only)
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) as tt where case2<=10
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) as tt where case2 between 5 and 10
case 3:取前10条记录
select id from mynumber fetch first 10 rows only
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) as tt where case3<=10
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) as tt where case3 between 5 and 10
oralce 例子
1. 最好还是利用分析函数
row_number() over ( partition by col1 order by col2 )
比如想取出100-150条记录,按照tname排序
select tname,tabtype from (
select tname,tabtype,row_number() over ( order by tname ) rn from tab
)
where rn between 100 and 150;
2. 直接使用rownum 虚列
select tname,tabtype from (
select tname,tabtype,rownum rn from tab where rownum <= 150
)
where rn >= 100;
使用序列不能基于整个记录集合进行排序,如果指定了order by子句,排序的的是选出来的记录集的排序.
create table mynumber(id int,name varchar(10));
insert into mynumber values(1,'no1');
insert into mynumber values(2,'no2');
insert into mynumber values(3,'no3');
insert into mynumber values(4,'no4');
insert into mynumber values(5,'no5');
insert into mynumber values(5,'no6');
insert into mynumber values(6,'no7');
insert into mynumber values(7,'no8');
insert into mynumber values(8,'no9');
insert into mynumber values(9,'no10');
insert into mynumber values(9,'no11');
insert into mynumber values(9,'no12');
insert into mynumber values(10,'no13');
insert into mynumber values(10,'no14');
insert into mynumber values(10,'no15');
insert into mynumber values(11,'no16');
insert into mynumber values(12,'no17');
insert into mynumber values(13,'no18');
select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber;
case1: 取前10条不同id记录,如果最后1条记录的ID 还有相同的,都要取出来。
select id,name from mynumber where id in (select id from (select distinct id from mynumber) tt where rownum<=10);
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) tt where case1<=10;
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) tt where case1 between 5 and 10;
case 2:取前10条记录,如果第10条记录的ID 还有相同的,都要取出来。
select * from mynumber where id in(select id from mynumber where rownum <=10);
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) tt where case2<=10;
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) tt where case2 between 5 and 10;
case 3:取前10条记录
select id,name from mynumber where rownum <=10;
select id,name from (select id,name,rownum rn from mynumber where rownum <= 10 ) where rn >= 5;
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) tt where case3<=10;
select * from (select id,name,RANK() over ( order by id ) case1,DENSE_RANK() over ( order by id ) case2,row_number() over ( order by id ) case3 from mynumber) tt where case3 between 5 and 10;
Mysql 例子
select id from mytable order by update_date desc limit 0,10
limit用法:
select * from tablename limit [offset],(rows)
注释:offset为偏移量
rows为要取出的记录条数
分享到:
相关推荐
2. **使用OFFSET**:跳过前几条记录,实现翻页功能。 **示例代码:** ```sql SELECT * FROM your_table LIMIT :offset, :limit; ``` 这里,`:offset`表示从哪一行开始显示,`:limit`表示每页显示的记录数。 #### ...
#### 一、数据库ORACLE 10g R2调优 ##### 1. 修改`open_cursors`和`session_cached_cursors`的参数值 - **命令行调整方法**: - 查看当前设置:`SQL> show parameter cursors` - 调整`open_cursors`的值:`SQL> ...
- **获取前10条记录**: ```sql SELECT TOP 10 * FROM TestTable WHERE ID NOT IN ( SELECT TOP 20 ID FROM TestTable ORDER BY ID ) ORDER BY ID; ``` ### Oracle分页方法 Oracle数据库提供了`ROWNUM`...
以下是针对Oracle、MySQL、SQL Server和DB2这四种常见数据库系统的分页实现方案的详细说明: 1. Oracle数据库: Oracle采用的是ROWNUM伪列来实现分页。以下是一个基本的分页查询示例: ```sql SELECT * FROM ( ...
本笔记将聚焦于DB2的学习,同时对比分析Oracle和MySQL两种流行数据库的分页实现,旨在帮助读者深入理解数据库分页机制。 首先,让我们了解一下分页在数据库中的重要性。在大数据量的查询场景下,一次性返回所有结果...
这条语句将从第11行开始提取10条记录。 Oracle分页 Oracle的分页语句使用ROWNUM关键字,语法为: ```sql SELECT * FROM (SELECT ROWNUM R, t.* FROM table_name WHERE ROWNUM ) WHERE R >= startIndex; ``` 其中,...
Oracle 10g与其他知名数据库管理系统,如IBM的DB2、Informix、Sybase、Microsoft的SQL Server和MySQL等一起,构成了企业级数据库解决方案的主体。这些系统各有特色,如DB2以其强大的事务处理能力著称,Informix在...
下面将详细介绍在Oracle、MySQL、SQL Server以及DB2这四种常见的数据库系统中如何实现查询指定数量的记录。 #### Oracle 在Oracle数据库中,可以使用`ROWNUM`伪列来限制查询结果的数量。`ROWNUM`为每一行返回一个...
oracle 甲骨文 获得最高认证级别的ISO标准安全认证,性能最高, 保持开放平台下的TPC-D和TPC-C的世界记录。但价格不菲 大型企业 db2 IBM DB2在企业级的应用最为广泛, 在全球的500家最大的企业中,几乎85%以上用DB2...
- 管理和优化简便:相比于Oracle、DB2、SQL Server等,MySQL在调试、管理和优化方面较为简单。 8. Char与Varchar的区别: - Char是固定长度的字符串类型,而Varchar是可变长度的。Char在存储时会预留指定长度的...
以DB2为例,假设我们想要获取第2页的数据,每页显示10条记录,可以使用以下SQL语句: ```sql SELECT * FROM ( SELECT employee_id, first_name, salary, ROW_NUMBER() OVER (ORDER BY employee_id) AS row_num ...
- **Oracle 10g**:引入网格计算技术,“g”代表网格(Grid)。 - **网格技术**:提高网络资源利用率,减少冗余和延迟。 - **Oracle课程目标**: - 安装与配置Oracle环境。 - 掌握数据库定义语言(DDL)操作。 - ...
《DB查询分析器》是一款功能强大、界面友好的数据库客户端工具,它支持多种数据库产品,包括但不限于Oracle、Sybase、DB2、Informix、MSSQL SERVER、MySQL、MS Access、FoxPro 和 Paradox等。无论是大型企业级数据库...
对于`Sys_option`表,从第10条记录开始取20条记录的SQL语句为: ```sql SELECT * FROM ( SELECT ROWNUM AS R, t1.* FROM Sys_option WHERE ROWNUM ) t2 WHERE t2.R >= 10 ``` 3. MySQL: MySQL数据库中,...
例如,从表 Sys_option 中从第 10 条记录开始检索 20 条记录,语句如下: ```sql SELECT * FROM ( SELECT TOP 20 * FROM ( SELECT TOP 29 * FROM Sys_option ORDER BY sys_id DESC ) t1 ) t2 ORDER BY ...
- **关系型数据库**(SQL,Structured Query Language):如 MySQL、Oracle、SQL Server、DB2、SQLite 等。这类数据库通过表之间的关系进行数据存储,支持结构化的查询语言。 - **非关系型数据库**(NoSQL,Not Only...
// 设置每页显示10条记录 List l = q.list(); ``` - `setFirstResult`: 设置从哪一条记录开始取。 - `setMaxResults`: 设置每页显示多少条记录。 #### 五、Oracle 分页实现 Oracle 使用 `rownum` 进行分页,但是...
常见版本有Oracle 9i、Oracle 10g、Oracle 11g等。 - **MySQL**:开源免费的小型数据库,适用于个人及小型项目。 - **DB2**:IBM出品的企业级数据库。 - **SQL Server**:微软公司的数据库产品,广泛应用于Windows...
- **行**(Row/Record):代表一条数据记录。 - **列**(Column/Field):代表数据的不同属性或特征。 ##### 1.5 SQL分类 SQL(Structured Query Language)是用于管理和操作关系数据库的标准语言,分为以下几个类别:...
IBM DB2、Oracle的Oracle和Mysql、微软的MS SQL。 Oracle的发展: 拉里·埃里森(Larry Ellison)仔细阅读了IBM的关系数据库的论文,敏锐意识到在这个研究基础上可以开发商用软件系统。他们决定开发通用商用数据库...