sql server怎么查询一个表的 第2条数据?

Ternence 2011-12-01 01:16:48
sql server怎么查询一个表的 第2条数据?新手求高手帮帮忙
...全文
3139 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ternence 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 fredrickhu 的回复:]

SQL code
select top 1 * from
(select top 2 * from tablename order by scoreproduct_id ) a
order by scoreproduct_id desc
[/Quote]

F姐的语句也能用 谢谢大家了
Ternence 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 fuxiyang 的回复:]

SQL code

select top 1 * from Scoreproduct where scoreproduct_id<>(select min(scoreproduct_id) from Scoreproduct )


最笨的方法
[/Quote]


额 结果对了。。 谢谢你
--小F-- 2011-12-01
  • 打赏
  • 举报
回复 1
select top 1 * from  
(select top 2 * from tablename order by scoreproduct_id ) a
order by scoreproduct_id desc
fuxiyang 2011-12-01
  • 打赏
  • 举报
回复

select top 1 * from Scoreproduct where scoreproduct_id<>(select min(scoreproduct_id) from Scoreproduct )

最笨的方法
Ternence 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 fuxiyang 的回复:]

那你直接按scoreproduct_id来查就行了啊
SQL code

set rowcount 2
select top 1 * from tb order by scoreproduct_id desc
[/Quote]

set rowcount 2
select top 1 * from Scoreproduct order by scoreproduct_id desc


Scoreproduct 表里面一共有5条数据 通过上面的查询语句 查询出来的是 最后的一条数据 ,不是倒数第2条啊。
F哥 如果您方面 您在您的表里执行下这个语句 看是不是也是我这个问题。。
fuxiyang 2011-12-01
  • 打赏
  • 举报
回复
那你直接按scoreproduct_id来查就行了啊

set rowcount 2
select top 1 * from tb order by scoreproduct_id desc
Ternence 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 fredrickhu 的回复:]

SQL code
select id=identity(int,1,1),* into #tb from tb
select * from #tb where id=2
[/Quote]

select id=identity(int,1,1),* into #tb from Scoreproduct
select * from #tb where id=2

无法使用 SELECT INTO 语句将标识列添加到表 '#tb',该表的列 'Scoreproduct_id' 已继承了标识属性。

我的表里有标识列了 所以插入不了了 是吗?
小弟是新手求F姐在帮帮忙
fuxiyang 2011-12-01
  • 打赏
  • 举报
回复
F姐,像这种,可以构建一个加了identity的视图不?然后直接从视图中取id=x的记录;
fuxiyang 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 alikeyount 的回复:]
引用 10 楼 fuxiyang 的回复:

要不读取前两条数据,追加一列identity之后插入临时表,然后再从临时表中取identity为2的记录;


我还不会插入临时表。。 但是你的意思我明白了
[/Quote]
按楼上F姐的语句就对了
--小F-- 2011-12-01
  • 打赏
  • 举报
回复
select id=identity(int,1,1),* into #tb from tb
select * from #tb where id=2
Ternence 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 fuxiyang 的回复:]

要不读取前两条数据,追加一列identity之后插入临时表,然后再从临时表中取identity为2的记录;
[/Quote]

我还不会插入临时表。。 但是你的意思我明白了
fuxiyang 2011-12-01
  • 打赏
  • 举报
回复
要不读取前两条数据,追加一列identity之后插入临时表,然后再从临时表中取identity为2的记录;
Ternence 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qianjin036a 的回复:]

select * from(
select row_number()over(order by (select 1))rn,* from tb
)t where rn=2
[/Quote]

执行的时候有错误。。 怎么回事。。。
Ternence 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fredrickhu 的回复:]

set rowcount 2
select top 1 * from tb order by id desc
[/Quote]

不行啊 我执行之后查询出来的 是倒序第一条数据 不是第2条 删除这个排序代码 查询出来的 也是第一条(正序)
麻烦再指点指点被。。
--小F-- 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fuxiyang 的回复:]
引用 3 楼 fredrickhu 的回复:
set rowcount 2
select top 1 * from tb order by id desc

如果没有主键、索引之类的,完全的乱序表,这个好像不行吧?
[/Quote]
恩 不行 不过如果本身是乱序的 不排序也没有意义了
fuxiyang 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fredrickhu 的回复:]
set rowcount 2
select top 1 * from tb order by id desc
[/Quote]
如果没有主键、索引之类的,完全的乱序表,这个好像不行吧?
yhui1989love 2011-12-01
  • 打赏
  • 举报
回复
F妹子就是威武 ...

rowcount 这个一直没用过完全没想起来啊!
-晴天 2011-12-01
  • 打赏
  • 举报
回复
select * from(
select row_number()over(order by (select 1))rn,* from tb
)t where rn=2
--小F-- 2011-12-01
  • 打赏
  • 举报
回复
set rowcount 2
select top 1 * from tb order by id desc
fuxiyang 2011-12-01
  • 打赏
  • 举报
回复
top 2里面not in top 1的
加载更多回复(1)

22,301

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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