sqlServer 查询表中31到40的记录,考虑id不连续的情况

SQL   查询表中31到40的记录,考虑id不连续的情况

写出一条sql语句输出users表中31到40记录(数据库为SQL Server,以自动增长的ID作为主键,注意ID可能不是连续的)?

--使用not in 
select top 10 * from users where id not in(select top 30 id from users order by id asc) order by id asc

--使用order by 
select * from (select top 10 * from  (select top 40 * from users order by id asc) as u order by u.id desc) as u1 order by u1.id asc

--使用开窗函数
select * from(select ROW_NUMBER() over(order by id) as num, * from users)as u where u.num between 31 and 40
原文地址:https://www.cnblogs.com/zlp520/p/3553576.html