zoukankan      html  css  js  c++  java
  • ORA-00933 UNION 与 ORDER BY

    原文:http://blog.csdn.net/lwei_998/article/details/6093807

    The UNION operator returns only distinct rows that appear in either result,

    while the UNION ALL operator returns all rows.
    The UNION ALL operator does not eliminate duplicate selected rows

    union返回不重复行。 union all 返回所有行。

    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.

    ORDER BY  是针对整个union的结果集order by,只能用在union的最后一个子查询中。

    SQL> SELECT employee_id, department_id
      2    FROM employees
      3   WHERE department_id = 50
      4   ORDER BY department_id
      5  UNION
      6  SELECT employee_id, department_id
      7    FROM employees
      8   WHERE department_id = 90
      9  ;

    SELECT employee_id, department_id
      FROM employees
     WHERE department_id = 50
     ORDER BY department_id
    UNION
    SELECT employee_id, department_id
      FROM employees
     WHERE department_id = 90

    ORA-00933: SQL 命令未正确结束

    SQL>
    SQL> SELECT employee_id, department_id
      2    FROM employees
      3   WHERE department_id = 50
      4  UNION
      5  SELECT employee_id, department_id
      6    FROM employees
      7   WHERE department_id = 90
      8    ORDER BY department_id;

    EMPLOYEE_ID DEPARTMENT_ID
    ----------------   --------------------
            120                              50
            121                              90

    也可以用在排序中使用列号,代替列名

    SQL> SELECT employee_id, department_id
      2    FROM employees
      3   WHERE department_id = 50
      4  UNION
      5  SELECT employee_id, department_id
      6    FROM employees
      7   WHERE department_id = 90
      8    ORDER BY 2
      9  ;

    EMPLOYEE_ID DEPARTMENT_ID
    ----------------   --------------------
            120                              50
            121                              90

  • 相关阅读:
    javascript中,一个js中的函数,第一句var _this = this;为什么要这样做?
    Sqlserver2012 sa账户登录添加其他账户
    ios模拟器快捷键
    XCode8.3真机调试设置
    1977-1998全国历年高考状元现状
    SQL修改数据表字段长度
    微信电脑版怎么修改信息提示音
    选择列表中的列无效,因为该列没有包含在聚合函数或 GROUP BY 子句中
    XML序列化和反序列化
    CoolFormat源代码格式化工具(转)
  • 原文地址:https://www.cnblogs.com/smallrock/p/3962614.html
Copyright © 2011-2022 走看看