怎样取出表中的第N条数据,头疼,在线等
create table tmp
(
a varchar(100)
b varchar(200)
)
go
insert into tmp
select 'row1 ', 'value111 '
union select 'row2 ', 'value222 '
union select 'row3 ', 'value333 '
go
请教怎样取出表中的第N条数据,
不要告诉我用 select * from tmp where a= 'rowX '
------解决方案--------------------eg:
--第二條
select TOP 1 * From (Select TOP 2 * From tmp Order By a) A Order By a Desc
------解决方案-------------------- --第一种方法
--第11条到第20条,共选出10条记录
select *
from (select top 10 * from (select top 20 * from 表名 order by ID) t1 order by ID desc) t2
order by ID
--第二种方法
--第11条到第20条,共选出10条记录
select top 10 *
from 表名
where ID> (select max(ID) from (select top 10 ID from 表名 order by ID) t1)
order by ID
------解决方案-----------------------如果你表有个自增ID的话那主好办
Select
Top 1 *
From (
Select Top 2 * From tmp) T
Order By id Desc
------解决方案--------------------create table tmp
(
a varchar(100),
b varchar(200)
)
go
insert into tmp
select 'row1 ', 'value111 '
union select 'row2 ', 'value222 '
union select 'row3 ', 'value333 '
go
select * from tmp a where (select count(1) from tmp where a <=a.a)=2 --数量
------解决方案--------------------/*取N到M条记录公式并按ID升序排列*/
Select
*
From (
Select
Top M-N+1 *
From (
Select Top M * From 表
) T
Order By ID Desc
) TT
Order By ID
/*取N到M条记录公式并按ID降序排列*/
Select
Top M-N+1 *
From (
Select Top M * From 表
) T
Order By ID Desc