SQL语句 问题

兔子侠客 2012-10-10 02:20:15
查找表中的 前5条 和后5条数据

我是这么写的

select * from(

(select top 5* from table order by id)t

union
(select top 5* from table order by id DESC)a
)

在查询分析器里显示 有错误

请问应该怎么改啊
多谢!!
...全文
243 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingxingbanyue 2012-10-12
  • 打赏
  • 举报
回复

select * from(
(select top 5 * from table)
union all
(select top 5 * from table order by id DESC)
) as T


xingxingbanyue 2012-10-12
  • 打赏
  • 举报
回复

select * from(
(select top 5 * from table)
union all
(select top 5 * from table order by id DESC)
)


另外,1. 只能在union的最后一个子查询中使用order by;

2. union会过滤重复数据,union all则显示不会过滤
兔子侠客 2012-10-11
  • 打赏
  • 举报
回复
楼上的几乎全有问题 都是小问题
(select top 5 * from table order by id DESC)a
)

不能加括号的

[Quote=引用 11 楼 的回复:]

SQL code
select * from(

(select top 5 * from table)t

union all
(select top 5 * from table order by id DESC)a
)
[/Quote]
风一样的大叔 2012-10-11
  • 打赏
  • 举报
回复
select * from(

(select top 5 * from table)t

union all
(select top 5 * from table order by id DESC)a
)
Showwang2011 2012-10-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

SQL code
SELECT * FROM
(select top 5 * from tests order by id )x
UNION ALL
SELECT * FROM
(select top 5 * from tests order by id desc)x
[/Quote]
我认为前五条那得by id 删掉有点多余,这样应该可以了。
lzk1018834925 2012-10-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

select * from(

(select top 5 * from table)t

union all
(select top 5 * from table order by id DESC)a
) c

或者
(select top 5 * from table)t
union all
(select top 5 * from table order by id ……
[/Quote]

顶一下
lzk1018834925 2012-10-10
  • 打赏
  • 举报
回复
select * from(

(select top 5* from table order by id)t

union
(select top 5* from table order by id DESC)a
)
???
你这是?神马错误?
我记得
select top 5* from table
union
select top 5* from table order by id DESC
这样可以么?
fjsnail 2012-10-10
  • 打赏
  • 举报
回复
select * from(

(select top 5 * from table)t

union all
(select top 5 * from table order by id DESC)a
) c

或者
(select top 5 * from table)t
union all
(select top 5 * from table order by id DESC)a
gua84607583 2012-10-10
  • 打赏
  • 举报
回复

with mytable as
(
select top 5 * from table order by id
union
select top 5 * from table order by id DESC
)
select * from mytable
hhzichen 2012-10-10
  • 打赏
  • 举报
回复
(select top 5 * from table)t

union all
(select top 5 * from table order by id DESC)a
zhangsuyunpk521 2012-10-10
  • 打赏
  • 举报
回复
select * from(

(select top 5 * from table)t

union all
(select top 5 * from table order by id DESC)a
)
dhy3610 2012-10-10
  • 打赏
  • 举报
回复
那个 5 和* 分开
six-years 2012-10-10
  • 打赏
  • 举报
回复
SELECT * FROM 
(select top 5 * from tests order by id )x
UNION ALL
SELECT * FROM
(select top 5 * from tests order by id desc)x

61,655

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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