当前位置: 移动技术网 > IT编程>数据库>MSSQL > sql 查询记录数结果集某个区间内记录

sql 查询记录数结果集某个区间内记录

2017年12月12日  | 移动技术网IT编程  | 我要评论

武汉最新新闻,泰坦尼克钥匙拍卖,g105m显卡

以查询前20到30条为例,主键名为id

方法一: 先正查,再反查
select top 10 * from (select top 30 * from tablename order by id asc) a order by id desc

方法二: 使用left join
select top 10 a.* from tablename a
left outer join (select top 20 * from tablename order by id asc) b
on a.id = b.id
where b.id is null
order by a.id asc

方法三: 使用not exists
select top 10 * from tablename a
where id not exists
(select top 20 * from tablename b on a.id = b.id)

方法四: 使用not in
select top 10 * from tablename
where id not in
(select top 20 id from tablename order by id asc)
order by id asc

方法五: 使用rank()
select id from
(select rank() over(order by id asc) rk, id from tablename) t
where rk between 20 and 30

中第五种方法看上去好像没有问题,查了下文档,当over()用于rank/row_number时,整型列不能描述一个列,所以会产生非预期的效果. 待考虑下,有什么办法可以修改为想要的结果.

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网