首頁  >  文章  >  資料庫  >  MySQL中OFFSET和FETCH的详解

MySQL中OFFSET和FETCH的详解

PHP中文网
PHP中文网原創
2017-03-29 17:43:595304瀏覽

OFFSET & FECTH 关键字与ORDER BY结合使用,实现对查询结果的分页

一、单独使用OFFSET:

示例:查询所有职员的信息,按雇佣日期排序并跳过前285条记录(共290条)

1 USE AdventureWorks2014
2 SELECT [BusinessEntityID],[HireDate],[JobTitle]
3 FROM [HumanResources].[Employee]
4 ORDER BY [BusinessEntityID]
5 OFFSET 285 ROWS

查询结果:

BusinessEntityID HireDate JobTitle
1 286    2013-05-30    Sales Representative
2 287    2012-04-16    European Sales Manager
3 288    2013-05-30    Sales Representative
4 289    2012-05-30    Sales Representative
5 290    2012-05-30    Sales Representative

二、OFFSET和FECTH结合使用:

示例:查询所有职员的信息,按雇佣日期排序并跳过前10条记录再显示之后的5条记录

1 USE AdventureWorks2014
2 SELECT [BusinessEntityID],[HireDate],[JobTitle]
3 FROM [HumanResources].[Employee]
4 ORDER BY [BusinessEntityID]
5 OFFSET 10 ROWS
6 FETCH NEXT 5 ROWS ONLY

查询结果:

BusinessEntityID HireDate JobTitle
1 11    2010-12-05    Senior Tool Designer
2 12    2007-12-11    Tool Designer
3 13    2010-12-23    Tool Designer
4 14    2010-12-30    Senior Design Engineer
5 15    2011-01-18    Design Engineer

以上就是MySQL中OFFSET和FETCH的详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn