select 马上结贴

bean_sql 2010-08-04 01:45:22
假设有表格
test(id1 varchar(50),id2 int)
0001 1
0002 2
0003 3
0004 4
0005 5


如何得到表里面的“第二条”数据 0002 2 不通过id1 和id2 字段~!
...全文
122 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hao1hao2hao3 2010-08-04
  • 打赏
  • 举报
回复
songsuqing0504 2010-08-04
  • 打赏
  • 举报
回复
if object_id('test') is not null
drop table test
create table test(id1 varchar(50),id2 int)
insert into test
select '0001',1 union all
select '0002',2 union all
select '0003',3 union all
select '0004',4 union all
select '0005',5
go

select id1,id2
from
(select id1,id2,row_number() over(order by getdate()) cn from test) w
where w.cn=2
--结果------
id1 id2
0002 2
水族杰纶 2010-08-04
  • 打赏
  • 举报
回复
非要这样
游标吧
百年树人 2010-08-04
  • 打赏
  • 举报
回复
如果不通过排序取第二条记录,似乎没什么意义
shixixi1987 2010-08-04
  • 打赏
  • 举报
回复

if exists(select * from sysobjects where [name]='tb')
drop table tb
go
create table tb
(
id1 varchar(50),
id2 int
)
go
insert into tb(id1,id2)
(
select '0001',1 union
select '0002',2 union
select '0003',3 union
select '0004',4 union
select '0005',5
)
go
select * from tb
go
select top 2 IDENTITY(INT,1,1) as num,* into #test from tb
go
select top 1 id1,id2 from #test order by num desc
go

id1 id2
-------------------------------------------------- -----------
0002 2
meinvqiujiu 2010-08-04
  • 打赏
  • 举报
回复
select top 1 * from (select top 2 * from test order by id1 desc) as table1
aaron_gl 2010-08-04
  • 打赏
  • 举报
回复
select c.* from (select row_number() over(order by select 1) as 'cnt',* from table) as c where c.cnt=2
TheGodOfGods 2010-08-04
  • 打赏
  • 举报
回复
昨天也有个类似的帖子

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧