当前位置:  数据库>oracle

Order By 的使用

    来源: 互联网  发布时间:2017-05-23

    本文导语: 一个sql中,union了几个子查询。单独执行每个子查询都没问题,但union后执行,报ORA-00904: "xxx": invalid identifier所union的各个子查询要有相同数量的列,且对应位置的列必须具有相同的数据类型;但列的名字可以不同。the diffrence betw...

一个sql中,union了几个子查询。单独执行每个子查询都没问题,但union后执行,报
ORA-00904: "xxx": invalid identifier

所union的各个子查询要有相同数量的列,且对应位置的列必须具有相同的数据类型;但列的名字可以不同。
the diffrence between UNION ALL and UNION is that UNION will attempt to eliminate duplicates.

Sql代码
  • SELECT supplier_city
  • FROM suppliers
  • WHERE supplier_name = 'IBM'
  • ORDERBY 1 DESC;
  • SELECT supplier_city FROM suppliers WHERE supplier_name = 'IBM' ORDER BY 1 DESC;


    This would return all records sorted by the supplier_city field in descending order, since the supplier_city field is in position #1 in the result set.



    union中order by的使用:
    You have to use the Order By at the end of ALL the unions。
    the ORDER BY is considered to apply to the whole UNION result(it's effectively got lower binding priority than the UNION).
    The ORDER BY clause just needs to be the last statement, after you've done all your unioning. You can union several sets together, then put an ORDER BY clause after the last set.
    所以,只能在union的最后一个子查询中使用order by,而这个order by是针对整个unioning后的结果集的。So:
    如果unoin的几个子查询列名不同,如

     
    Sql代码
  • select supplier_id, supplier_name
  • from suppliers
  • UNION
  • select company_id, company_name
  • from companies
  • ORDERBY ?;
  • select supplier_id, supplier_name from suppliers UNION select company_id, company_name from companies ORDER BY ?;


    这里的问号如果是company_name,则执行整个查询会报“company_name:invalid identifier”(当然,单独执行第二个含order by的子查询是没有问题的);这是因为unioning后结果集的列名是以第一个参加union的子查询的列名为准的;order by针对的是整个unioning后的结果集。对整个查询结果来说,无”company_name“这个字段
    如果是supplier_name,则单独执行第二个含order by的子查询是会报“supplier_name:invalid identifier”的,而执行整个查询是没有问题的,因为order by针对的是unioning后的整个结果集,而这“整个结果集”是有supplier_name这列的(以第一个union子查询的列名作为unioning后整个结果集的列名)

    为了避免这样事情的发生,可以:
    1 使用列序号代替实际列名。如:

     
    Sql代码
  • select supplier_id, supplier_name
  • from suppliers
  • UNION
  • select company_id, company_name
  • from companies
  • ORDERBY 2;
  • select supplier_id, supplier_name from suppliers UNION select company_id, company_name from companies ORDER BY 2;


    2 为unoin的各个子查询使用相同的列名,如:

     
    Sql代码
  • select supplier_id as id, supplier_name asname
  • from suppliers
  • UNION
  • select company_id as id, company_name asname
  • from companies
  • ORDERBYname;
  • select supplier_id as id, supplier_name as name from suppliers UNION select company_id as id, company_name as name from companies ORDER BY name;



    这样,不管是执行整个查询还是单独执行包含order by的最后一个union子查询,都不会有问题。



     
    I have two tables, TableA and TableB defined as follows,

    TableA
    A1 int
    A2 int
    A3 int

    TableB
    B1 int
    B2 int
    B3 int

    If I try to run this query, SQL Server says syntex failed at the Order By clouse. Is such Order by not allowed in SQL, Any other way to achieve this?
    Sql代码
  • (Select A1, A2 from TableA)
  • UnionAll
  • (Select B1, B2 from TableB Orderby B3)
  • (Select A1, A2 from TableA) Union All (Select B1, B2 from TableB Order by B3)

    Any help will be appreciated.


    A:

    引用
    First of all, you can not order by a column that is not included in your SELECT list(我注:这句话是错误的;可以order by一个不在select列表中的column). Secondly, when performing a UNION query the ORDER BY clause must be(我注:not “must be”!) a column index not a column name, because a UNION query does not have column headings (although SQL Server(我注:此处泛指DBMS) pretends that it has by picking the column names used in the first queryalthough this is not ANSI compliant]). Assuming you want to order the second column (A2 and B2) your query should look like this:
    Code:
    Sql代码
  • SELECT A1, A2
  • FROM TableA
  • UNIONALL
  • SELECT B1, B2
  • FROM TableB
  • ORDERBY 2
  •  SELECT A1, A2 FROM TableA UNION ALL SELECT B1, B2 FROM TableB ORDER BY 2


    Conceptually, ORDER BY works by producing the final query table with all the queries joined together (if it is a UNION query), then it orders the query results and does not care about what is in the database.


        
     
     

    您可能感兴趣的文章:

  • 关于java中执行sql语句使用order by的问题!
  • oracle使用order by排序null值如何处理
  • Mysql联合查询UNION和Order by同时使用报错问题的解决办法
  • mysql中order by与group by的区别
  • 深入解析mysql中order by与group by的顺序问题
  • 如何在CMP中处理Order by 和 Group by
  • SQL-ORDER BY 多字段排序(升序、降序)
  • group by,having,order by的用法详解
  • 深度分析mysql GROUP BY 与 ORDER BY
  • 在sql中不指定Order by排序是按照主键吗
  • MySQL Order By用法分享
  • mysql order by 失效了?
  • order by newid() 各种数据库随机查询的方法
  • Mysql中order by、group by、having的区别深入分析
  • PHP MySQL Order By 关键词的用法
  • sql语句之ORDER BY 子句用法
  • MySQL Order By语法介绍
  • MYSQL随机抽取查询 MySQL Order By Rand()效率问题
  • MySQL Order By Rand()效率分析
  • MySQL Order by 语句用法与优化详解
  • sql语句排序子句order by用法实例
  • SQL order by ID desc/asc加一个排序的字段解决查询慢问题
  • MySQL查询优化:连接查询排序limit(join、order by、limit语句)介绍
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • java命名空间java.nio类charbuffer的类成员方法: order定义及介绍
  • ThinkPHP CURD方法之order方法详解
  • java命名空间java.nio类bytebuffer的类成员方法: order定义及介绍
  • String sqlstr="select max(RECN) as recn from order_info";
  • java命名空间java.nio类intbuffer的类成员方法: order定义及介绍
  • union组合结果集时的order问题
  • java命名空间java.nio类doublebuffer的类成员方法: order定义及介绍
  • ThinkPHP CURD方法之order方法教程详解
  • java命名空间java.nio类shortbuffer的类成员方法: order定义及介绍
  • 怎样设置xml中<Order pono="PO0626-003237">的pono="PO0626-003237">的属性
  • java命名空间java.nio类longbuffer的类成员方法: order定义及介绍
  • TOP N与Row_Number()分页因Order by排序规则不同引发的bug
  • java命名空间java.nio类floatbuffer的类成员方法: order定义及介绍
  • MySQL 通过索引优化含ORDER BY的语句
  • java命名空间javax.swing.event枚举rowsorterevent.type的类成员方法: sort_order_changed定义及介绍
  • MySQL ORDER BY 的实现分析
  • java命名空间java.util.jar接口pack200.packer的类成员方法: keep_file_order定义及介绍
  • mysql分组取每组前几条记录(排名) 附group by与order by的研究
  • java命名空间java.lang类string的类成员方法: case_insensitive_order定义及介绍
  • CSS order 属性


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,