oracle中的子查询中为什么不能有order by?那该用什么方法来解决子查询中必须要ORDER BY的问题?

解决方案 »

  1.   

    ?你说的是下面这种子查询么:
    select a.col1 from a where a.col2 in (select b.col2 from b order by b.col1);
    这里是不能用order by,这种情况下的子查询,只是一个集合,不需要order by ;但是这种情况下的子查询可以用order by:
    select a.col1, b.col2
    from a,
         (select c.col1 from c  order by c.col1) b不知道你是需要order by的需求到底是什么样子的,是否可以通过其它的方法解决呢。
      

  2.   

    1):
    select @str=@str+Value+';'
    from
    (
        select Value,[Order] from dbo.SMS_SpecialConfigData data
        join  dbo.SMS_Config co
        on co.ItemID=data.ConfigID
        where Time='2008-5-12' and OrgCode='GZ/'
        order by [Order]
    )temp2):
    select @str=@str+Value+';'
    from
    (
        select Value,[Order] from dbo.SMS_SpecialConfigData data
        join  dbo.SMS_Config co
        on co.ItemID=data.ConfigID
        where Time='2008-5-12' and OrgCode='GZ/'
    )temp
    order by [Order]第一个出错,第二个又得不出想要的结果。但是这两个好像没什么区别啊?为什么
      

  3.   


    --2005的话试试
    select @str=@str+Value+';'
    from
    (
      select Value,[Order],row_number()over(order by Order )row_ from dbo.SMS_SpecialConfigData data
      join dbo.SMS_Config co
      on co.ItemID=data.ConfigID
      where Time='2008-5-12' and OrgCode='GZ/'
    )temp
      

  4.   

    子查询中就是不能有 order by。从逻辑上说,order by 应该是查询过程的最后一步。