a442579302 2014-05-22 17:54
浏览 1130
已采纳

关于SQL子查询中使用order by的问题

最近回头看以前的数据库教材《数据库系统概论》人大 王珊 版的

有一段叙述:“需要特别指出的是,子查询的select语句不能使用 order by子句,order by子句只能对最终查询结果排序”

但是正好之前练习碰到个场景,就在子句中用了order by ,结果也是正确的。

所以我很疑惑,是书上说错了还是什么原因
下面是SQL

[code="java"]select userid from (select userid,min(createtime) from LT_BIZORDER t
where (t.status in (1,2,3,4,5) or (t.status=6 and t.endstatus in (0,1,2)))
and createtime > to_date('2014-06-01','yyyy/MM/dd')
group by t.userid order by min(createtime) asc)
where rownum<=10000[/code]

  • 写回答

6条回答 默认 最新

  • lovejiegirl1 2014-05-22 20:43
    关注

    [code="java"]

    1.子查询是可以使用order by
    2.我测试了 select * from (子查询 order by)
    select * from table A where id not in (子查询 order by )

    都是可以的

    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;
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化