关于order by的处理

bartholomew4 2011-07-25 02:43:35
select top 10 convert(varchar(10),orderId),p.Pro_Name,num,price,Amount from (
select orderId,num,price,Amount,proId from
(select top 30 orderId,num,price,Amount,proId from orderDetails ) a order by a.orderid desc)o
inner join Product p on o.proId=p.Pro_ID order by o.orderid asc


消息 1033,级别 15,状态 1,第 3 行
除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。



求解决办法
...全文
130 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
bluesmiler 2011-07-25
  • 打赏
  • 举报
回复
你加order by 前面要指定top的,所以再加个top 30 上去,
不要直接select orderId,num,price,Amount,proId from (select top 30 orderId,num,price,Amount,proId from orderDetails ) a order by a.orderid desc
bluesmiler 2011-07-25
  • 打赏
  • 举报
回复

试试这个
select top 10 convert(varchar(10),orderId),p.Pro_Name,num,price,Amount from (
select top 30 orderId,num,price,Amount,proId from
(select top 30 orderId,num,price,Amount,proId from orderDetails ) a order by a.orderid desc)o
inner join Product p on o.proId=p.Pro_ID order by o.orderid asc
bluesmiler 2011-07-25
  • 打赏
  • 举报
回复
select  top 10 convert(varchar(10),orderId),p.Pro_Name,num,price,Amount from (
select top 30 orderId,num,price,Amount,proId from
(select top 30 orderId,num,price,Amount,proId from orderDetails ) a order by a.orderid desc)o
inner join Product p on o.proId=p.Pro_ID
bartholomew4 2011-07-25
  • 打赏
  • 举报
回复
排序不正确,应该是按照ASC排列的,而且你的这个语句只能查到5条(我也不知道为什么)
AcHerat 元老 2011-07-25
  • 打赏
  • 举报
回复

select convert(varchar(10),orderId),p.Pro_Name,num,price,Amount from (
select top 10 orderId,num,price,Amount,proId from
(select top 30 orderId,num,price,Amount,proId from orderDetails ) a order by a.orderid desc)o
inner join Product p on o.proId=p.Pro_ID
bartholomew4 2011-07-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yubofighting 的回复:]

SQL code


select top 10 convert(varchar(10),orderId),p.Pro_Name,num,price,Amount from (
select orderId,num,price,Amount,proId from
(select top 30 orderId,num,price,Amount,proId from……
[/Quote]不是视图,是表
bartholomew4 2011-07-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 acherat 的回复:]

select top 10 convert(varchar(10),orderId),p.Pro_Name,num,price,Amount from (
select orderId,num,price,Amount,proId from
(select top 30 orderId,num,price,Amount,proId from orderDeta……
[/Quote]
后面的那不行,是把整个表先排序了在TOP 30,我要的是在不排序的情况下TOP30,再取21-30条记录
AcHerat 元老 2011-07-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 bartholomew4 的回复:]

引用 2 楼 yubofighting 的回复:

SQL code


select top 10 convert(varchar(10),orderId),p.Pro_Name,num,price,Amount from (
select orderId,num,price,Amount,proId from
(select top 30 orderId,num,price,A……
[/Quote]

看4楼SQL CODE里的!
bartholomew4 2011-07-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yubofighting 的回复:]

SQL code


select top 10 convert(varchar(10),orderId),p.Pro_Name,num,price,Amount from (
select orderId,num,price,Amount,proId from
(select top 30 orderId,num,price,Amount,proId from……
[/Quote]
实现不了功能,我要的是TOP30里的最后10条
AcHerat 元老 2011-07-25
  • 打赏
  • 举报
回复
select top 10 convert(varchar(10),orderId),p.Pro_Name,num,price,Amount from (
select orderId,num,price,Amount,proId from
(select top 30 orderId,num,price,Amount,proId from orderDetails ) a order by a.orderid desc)o
inner join Product p on o.proId=p.Pro_ID order by o.orderid asc

红色部分去掉!或者

select top 10 convert(varchar(10),orderId),p.Pro_Name,num,price,Amount from (
select top 30 orderId,num,price,Amount,proId from orderDetails order by orderid desc)o
inner join Product p on o.proId=p.Pro_ID order by o.orderid asc
叶子 2011-07-25
  • 打赏
  • 举报
回复
你这个是视图吧?

--把order by 去掉
--再调用视图的时候加上
select * from yourview order by orderid asc

yubofighting 2011-07-25
  • 打赏
  • 举报
回复


select top 10 convert(varchar(10),orderId),p.Pro_Name,num,price,Amount from (
select orderId,num,price,Amount,proId from
(select top 30 orderId,num,price,Amount,proId from orderDetails ) a )o
inner join Product p on o.proId=p.Pro_ID order by orderId



快溜 2011-07-25
  • 打赏
  • 举报
回复
order by 只能在外层查询

34,590

社区成员

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

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