SQL的子查询可以使用order by子句吗?

shijie8332899 2009-05-29 11:25:21
select * from table1 from (select field2 from table2 order by field2 desc) as table 加order by 的時候會出錯。不知各位有沒有好的辦法。一定要order子句,不能刪除。有沒有好的辦法
...全文
2594 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
liurunbin 2012-06-22
  • 打赏
  • 举报
回复
嗯,楼上说的有道理。
lovehewenyu 2012-06-21
  • 打赏
  • 举报
回复
1、首先子查询中可以使用order by!
2、楼主的问题,我觉得是 as 用法错误 ,跟order by 是否应用子查询没有关系。修改成为即可 select * from table1 from (select field2 from table2 order by field2 desc) table
3、下面截取官档一段order by 和 rownum 一起用的范例!


If you embed the ORDER BY clause in a subquery and place the ROWNUM condition in the top-level query, then you can force the ROWNUM condition to be applied after the ordering of the rows. For example, the following query returns the employees with the 10 smallest employee numbers. This is sometimes referred to as top-N reporting:

SELECT * FROM
(SELECT * FROM employees ORDER BY employee_id)
WHERE ROWNUM < 11;
liaomin416100569 2011-09-22
  • 打赏
  • 举报
回复
这个oracle的怎么解决 非常郁闷
ai_li7758521 2009-05-30
  • 打赏
  • 举报
回复
order by 是不允许放子查询的,除非加top。
一般说来,在内层排序可以拿到外不来排序。

select *
from table1,(select field2 from table2) as [table]
order by [table].field2 desc
shijie8332899 2009-05-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lihan6415151528 的回复:]
引用楼主 shijie8332899 的帖子:
select * from table1 from (select field2 from table2 order by field2 desc) as table 加order by 的時候會出錯。不知各位有沒有好的辦法。一定要order子句,不能刪除。有沒有好的辦法



select * from table1 from....



SQL code
select * from (select field2 from table2 order by field2 desc) as Tab
[/Quote]
那個我打錯的。子查詢不能加order的
shijie8332899 2009-05-30
  • 打赏
  • 举报
回复
樓上的已經給出正確的答案了。謝謝各位的回覆!
you_tube 2009-05-29
  • 打赏
  • 举报
回复
select  *  from (select  field2  from  table2 order by field2 desc) as Tab
sunbaopan 2009-05-29
  • 打赏
  • 举报
回复
select * from (select top 1 field2 from table2 order by field2 desc) as table
ks_reny 2009-05-29
  • 打赏
  • 举报
回复

select * from (select field2 from table2 order by field2 desc) as a
csdyyr 2009-05-29
  • 打赏
  • 举报
回复
select * from (select TOP 100 PERCENT field2 from table2 order by field2 desc) as tableName
Zoezs 2009-05-29
  • 打赏
  • 举报
回复

select * from table1 from (select field2 from table2 order by field2 desc) as table

两个from当然出错。
是不是这样

select * from (select field2 from table2 order by field2 desc) as table
JonasFeng 2009-05-29
  • 打赏
  • 举报
回复
order by 不能放在子句里。


select * from (select field2 from table2) as table
order by table.field2 desc
lihan6415151528 2009-05-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 shijie8332899 的帖子:]
select * from table1 from (select field2 from table2 order by field2 desc) as table 加order by 的時候會出錯。不知各位有沒有好的辦法。一定要order子句,不能刪除。有沒有好的辦法
[/Quote]



select * from table1 from....



select * from (select field2 from table2 order by field2 desc) as Tab
  • 打赏
  • 举报
回复
子查询不能用order by, 因为在sql执行时到order by这句返回的是指针, 并没有真正返回tmp table.
如果有top n 的话, 可以有order by.
HuSongLove 2009-05-29
  • 打赏
  • 举报
回复
高手们都说了!
SQL77 2009-05-29
  • 打赏
  • 举报
回复

select * from (select field2 from table2 ) as [table ]
order by field2 desc

??
SQL77 2009-05-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 csdyyr 的回复:]
select * from (select TOP 100 PERCENT field2 from table2 order by field2 desc) as tableName
[/Quote]
支持!!
SQL77 2009-05-29
  • 打赏
  • 举报
回复
字查询使用ORDER BY 会提示要指定TOP

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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