先创建表及插入测试数据,以 MySQL为例,中如下:
--建表
CREATE TABLE `table1` (
`id` int(11) NOT NULL,
`name` varchar(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
--插入测试数据
insert into table1 values
(1,'beibei'),
(2,'jack'),
(3,'tom'),
(4,'andy'),
(5,'liu'),
(6,'jacky');
--查询
--SELECT * FROM table1
id name
1 beibei
2 jack
3 tom
4 andy
5 liu
6 jacky
如果查询第3行到第5行,即查询结果为:
id name
3 tom
4 andy
5 liu
MySQL:
--2为行的索引,3为查询出来的行的总数
SELECT * FROM table1 limit 2,3;
SQL Server:
--此语句未经验证,如有错误请指正!
SELECT * FROM (SELECT TOP 3 * FROM (SELECT TOP 5 * FROM table1) ORDER BY id DESC) ORDER BY id ASC;
Oracle:
SELECT * FROM (SELECT rownum rn,t.* FROM table1 t) WHERE rn BETWEEN 3 AND 5;
--或者使用分析函数
SELECT * FROM (SELECT t.*, ROW_NUMBER() OVER (ORDER BY id) rn FROM tb t) WHERE rn BETWEEN 3 AND 5
版权声明:本文为博主原创文章,未经博主允许不得转载。