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


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