SQL limit关键字

一、Limit基本用法

在MySQL中,limit关键字用来在查询结果集中,选择指定的行返回,常常用来实现翻页功能。

有三种常见用法:

1、

select * from table limit 10;	// limit n;		返回查询结果的前n条数据



2、

select * from table limit 0,10;	//limit offset, n;	返回从offset + 1开始的n条数据



select * from table limit 1,10;	




3、select * from table limit 10,-1; //返回从第11条数据开始的所有数据

查了些资料提示有这种用法,但是我的MySQL提示有语法错误,可能是数据库版本不对吧。


二、SQL server中实现翻页

SQLserver不支持limit关键字,要实现相应的功能,需使用top关键字。

例如:

查询前10行数据:

select top 10 * from table order by id;


查询10~20行数据:

select top 10 * from table 
where id not in 
	(select top 10 id from table order by id) 
order by id


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章