Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)归海一刀published in(发表于) 2014/2/3 6:44:14 如何用一条sql取得第10到第20条的记录?_[SQL Server教程]
因为id可能不是连续的,所以不能用取得10有三种方法可以实现:一、搜索前20条记录,指定不包括前10条语句:select top 20 * from tbl where id not in (select top 10 id from tbl)二、搜索记录生成临时表,建立临时表的自增id。通过取得自增id的10语句:select identity(int,1,1) as id,* into #temp from tbl;select * from #temp where id between 10 and 20第二个方法实际上是两条语句,但你可以让他连续执行,就像一条语句一样完成任务。三、朋友们觉得第一种方法效率太低,经过讨论,得出第三种方法:语句:SELECT TOP 10 * FROM (SELECT TOP 20 * FROM tblORDER BY id) as tbl2 ORDER BY tbl2.id DESC来源:http://www.cnblogs.com/gofficer
赞