导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

在SQLServer中如何读取一张表最末五行数据?

crazy_bo 2007-12-13 01:17:18
谢谢
...全文
163 点赞 收藏 12
写回复
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyl918 2007-12-13
呵呵,学习了,给点分吧,呵呵
回复
daviszr_1979 2007-12-13
Select top 5 * From Test Where ID Not In Select Top (Select Count(*)-5 From Test) From Test
回复
shirley_yue 2007-12-13
STUDY
回复
fqbnet2050 2007-12-13
不错,学习一下
回复
JL99000 2007-12-13
老乌龟的方法更有特点
老乌龟你不被关起来了吗,杂的,出来了啊
回去吧

回复
fwacky 2007-12-13

select top 5 * from table order by [你要排序的字段] desc
回复
dawugui 2007-12-13
--1,按ID排序
select top 5 * from tb order by id
select top 5 * from tb order by id desc

--2不排序,如何从表中用SELECT语句提取最后10条数据
declare @num as int
select @num = count(*) from authors
set @num = @num - 10

declare @sql as varchar(200)
set @sql = 'select * from authors where au_id not in (select top ' + cast(@num as char) + ' au_id from authors)'
exec (@sql)
回复
中国风 2007-12-13
2005时用:
selett top 5 *
from
(select *,row=row_number()over(order by ID)
from T)TT
order by row desc
回复
hui_hui_2007 2007-12-13
前提是必须要有一个惟一标识列。否则没有严谨的实现办法。
回复
-狙击手- 2007-12-13
如果有唯一标识列
如楼上

如果没有
生成中间表select identity(int,1,1) as id * into # from tb
,再select top 5 ... order by id desc
回复
wgsasd311 2007-12-13
/*一条SQL语句可能实现不了,因为你的问题必须要用到排序,而排序必须要指定按哪一列或哪几列组合或是表达式.*/
select * ,identity(int,1,1) as pxid into #tmp from tb
select top 5 * from #tmp order by pxid desc
drop table #tmp


回复
wzy_love_sly 2007-12-13
select top 5 * from table order by id desc
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告