最后5条

qddmha 2003-08-26 10:54:11
怎样用sql语句直接获得结果集中最后5条纪录?
没分了,先给10分
...全文
41 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
z5wjz 2003-08-26
  • 打赏
  • 举报
回复
例如:

select * from products p1
where (select count(*) from products p2
where p2.productid>=p1.productid )<=5
z5wjz 2003-08-26
  • 打赏
  • 举报
回复
不好意思写错了,应该是
select identity(int,1,1) as id,* into #aa from yourtable
go
select top 5 * from #aa
order by id desc
go
drop table #aa
liuyun2003 2003-08-26
  • 打赏
  • 举报
回复
select * from yourtable where pk not in (select top row-5 pk from yourtable)
思路:先找到总行数减五的数据,然后在整个表里找不在这个结果集的数据。可是速度不会快。
yujohny 2003-08-26
  • 打赏
  • 举报
回复
用结果集导入一个临时表,临时表设个自动递增的ID字段
然后select top 5 * from TempTable ORDER BY ID DESC
z5wjz 2003-08-26
  • 打赏
  • 举报
回复
select identity(id,1,1),* into #aa from yourtable
go
select top 5 * from #aa
order by id desc
go
drop table #aa
english218 2003-08-26
  • 打赏
  • 举报
回复
select top 5 * from yourtablename order by id desc

22,206

社区成员

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

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