在sql server 2005中如何查询从第几行到第几行的值 5
展开全部
这个事首先一定得要有标识字段,否则还真没法办.有了标识才能排序,能排序才能谈得上取第几行,还要借助一个临时表,下面给个例子
假设取第10行到第20行的数据
select top 20 * into t_tmp from t_1 order by id asc
select top 10 * from t_tmp roder by id desc
这两条语句应该可以写在一行而不用借助临时表,我写不出来,就像下面这句是错的
select top 10 * from (select top 20 * from t_1 order by id asc) t_1
roder by id desc
假设取第10行到第20行的数据
select top 20 * into t_tmp from t_1 order by id asc
select top 10 * from t_tmp roder by id desc
这两条语句应该可以写在一行而不用借助临时表,我写不出来,就像下面这句是错的
select top 10 * from (select top 20 * from t_1 order by id asc) t_1
roder by id desc
展开全部
使用开窗函数最为简便
select * from
(select row_number() over(order by 你要排序的字段名 ) as rn,a.* from tableName a )aa
where aa.rn >=起始行号 and aa.rn<=结束行号
select * from
(select row_number() over(order by 你要排序的字段名 ) as rn,a.* from tableName a )aa
where aa.rn >=起始行号 and aa.rn<=结束行号
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
取n到m条记录的语句
1.
select top m * from tablename where id not in (select top n * from tablename)
2.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
set rowcount n
select * from 表变量 order by columnname desc
3.
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc
谢谢加分啊!
1.
select top m * from tablename where id not in (select top n * from tablename)
2.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
set rowcount n
select * from 表变量 order by columnname desc
3.
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc
谢谢加分啊!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select * from test where primaryKey not in ( select top n a.primaryKey from test a order by primaryKey)
and name in (select top m a.primaryKey from test a order by primaryKey)
n到m的记录
and name in (select top m a.primaryKey from test a order by primaryKey)
n到m的记录
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select * from 表 where id between 值 and 值
追问
我没有用id这列
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |