如何从一个sqlserver表里,取最后的5条记录

csdn3ks 2003-08-18 11:22:28
该表没有顺序号,也没有可排序的字段。
从顶上取 是 select a top 5 from aaa

如何从一个sqlserver表里,取最后的5条记录呢?
...全文
520 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
FranklinBHU 2003-08-19
  • 打赏
  • 举报
回复
学习ing,做个记号
ysycrazy 2003-08-19
  • 打赏
  • 举报
回复
使用临时表:
select identity(int,1,1) as ID,* into #tb_temp from aaa
select top 5 * from #tb_temp order by id desc
chuxin1 2003-08-19
  • 打赏
  • 举报
回复
Gz
caiyunxia 2003-08-18
  • 打赏
  • 举报
回复
select top 5 * from table order by pk desc
pengdali 2003-08-18
  • 打赏
  • 举报
回复
或:


select *,IDENTITY(int, 1,1) ID into #temp from 表

select top 5 * from #temp order by a desc
txlicenhe 2003-08-18
  • 打赏
  • 举报
回复
Select identity(int,1,1) as id,* into #tmp from aaa
select top 5 * from #tmp order by id desc
drop table #tmp
pengdali 2003-08-18
  • 打赏
  • 举报
回复
declare @a int,@b varchar(8000)
select @a=count(*)-5 from aaa
set @b='select * from where 主键 not in (select top '+cast(@a as varchar(10))+' 主键 from 表)'

exec(@b)
zjcxc 元老 2003-08-18
  • 打赏
  • 举报
回复
那就只好用临时表

select id=identity(int,1,1),* into #tb from 你的表
delect top 5 * from #tb order by id desc
drop table #tb
CrazyFor 2003-08-18
  • 打赏
  • 举报
回复
select * ,IDENTITY(int, 1,1) AS ID
into #temp from chkdb

select * from (select top 5 * from #temp order by id desc)tem order by id
dlkfth 2003-08-18
  • 打赏
  • 举报
回复

select * ,IDENTITY(int, 1,1) AS ID
into #temp from chkdb

select top 5 * from #temp order by id desc
yun198183 2003-08-18
  • 打赏
  • 举报
回复
学习
zclxyh 2003-08-18
  • 打赏
  • 举报
回复
使用临时表。
select *,IDENTITY(int, 1,1) ID into #temp from 表名

select top 5 * from #temp order by id desc

34,874

社区成员

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

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