查出一个表的前20条记录和后20条记录

upeveryday 2005-09-05 09:15:59
查出一个表的前20条记录和后20条记录
...全文
217 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
churchatp1 2005-09-06
  • 打赏
  • 举报
回复
如果你要排序的列是可以排序的可以这么做:
select top 20 * from tablename order by 列名
union all --不删除重复行
select top 20 * from tablename order by 列名 DESC
前提是要该列可以排序

iwl 2005-09-06
  • 打赏
  • 举报
回复
select top 20 * from tablename order by 主键
union
select top 20 * from tablename order by 主键 DESC
dbbbs 2005-09-06
  • 打赏
  • 举报
回复
select top 20 * from tablename order by 主键
union all --不删除重复行
select top 20 * from tablename order by 主键 DESC
zhangyang555 2005-09-06
  • 打赏
  • 举报
回复
select id =identity(int,1,1),* into #t from table1
select top 20 * from #t --前20条
select top 20 * from #t order bu id desc --后20条

如果要一起显示:
select top 20 * from #t
union all --如果不允许重复记录就不要加all
select top 20 * from #t order bu id desc --后20条
drop table #t
云中客 2005-09-06
  • 打赏
  • 举报
回复
select top 20 * from tablename order by 主键
union
select top 20 * from tablename order by 主键 DESC
同意这个办法
zhangzs8896 2005-09-06
  • 打赏
  • 举报
回复
--假如可确定顺序的列是col1
select top 20 * from tablename order by col1
select top 20 * from tablename order by col1 desc

一起显示的话,union一下就可以了。
NOKIA_LIU 2005-09-06
  • 打赏
  • 举报
回复
呵呵。。the_secular_bird(最后的晚餐) 真是有心人。
楼主的意思是把前20条和后20天一起显示出来。。呵呵学习ing...
fandy81 2005-09-06
  • 打赏
  • 举报
回复
the_secular_bird(最后的晚餐) 的语句比较可行!
languang122 2005-09-06
  • 打赏
  • 举报
回复
select top 20 * from tablename order by 主键
union all --不删除重复行
select top 20 * from tablename order by 主键 DESC
这种方式我实验了一下,单独执行前一个select_statement 或后一个select_statement 没有问题
一加上union后为什么会提示union 附近有语法错误呢?
the_secular_bird 2005-09-06
  • 打赏
  • 举报
回复

join 表名
id 主键名称


select * from [join] where [id] in ((select top 20 [id] from [join] order by ([id]))) or [id] in ((select top 20 [id] from [join] order by ([id]) desc))



zzxiaoma 2005-09-05
  • 打赏
  • 举报
回复
select top 20 *from table1 union all
select top 20 *from table1 desc
lxzm1001 2005-09-05
  • 打赏
  • 举报
回复
select top 20 * from tablename
rivery 2005-09-05
  • 打赏
  • 举报
回复
--假如可确定顺序的列是col1
select top 20 * from tablename order by col1
select top 20 * from tablename order by col1 DESC

34,576

社区成员

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

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