原文: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

ORA-00933 UNION 与 ORDER BY的更多相关文章

  1. 让UNION与ORDER BY并存于SQL语句当中

    在SQL语句中,UNION关键字多用来将并列的多组查询结果(表)合并成一个结果(表),简单实例如下: SELECT [Id],[Name],[Comment] FROM [Product1] UNIO ...

  2. mysql 错误 1221 Incorrect usage of union and order by

    今天有个项目出现了问题 问题原因是union和order by 的问题.关于这个问题的解决方案可以猛击下面的链接. http://blog.csdn.net/gtuu0123/article/deta ...

  3. mysql中的union和order by、limit

      我有一个表 CREATE TABLE `test1` (  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  `name` varchar(20) N ...

  4. MySQL中union和order by一起使用的方法

    MySQL中union和order by是可以一起使用的,但是在使用中需要注意一些小问题,下面通过例子来说明.首先看下面的t1表. 1.如果直接用如下sql语句是会报错:Incorrect usage ...

  5. 同时使用Union和Order by问题(ORA-00933错误)解决

    之前,同事在编写视图的过程中遇到这样了这个错误.我把简化后的语句整理如下: 1: select 2: '2016' as nf, 3: qxdm, 4: round(sum(tbdlmj)/10000 ...

  6. MySql union与order by

    [MySql union与order by] 如果您想使用ORDER BY或LIMIT子句来对全部UNION结果进行分类或限制,则应对单个地SELECT语句加圆括号,并把ORDER BY或LIMIT放 ...

  7. MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)

    我在一个业务中采用了按月的分表策略,当查询的条件跨月的时候,使用了union all汇总2个表的数据,并按插入时间倒序排列.查询并不复杂,但是当执行的时候却报错了. SELECT * FROM `ta ...

  8. union 和order by 使用时排序不正确

    静态专题和APP版专题(order by不起作用): [query] sql=(select sp_f13577,sp_f13576 from sp_t113 where url_1 not like ...

  9. mysql Incorrect usage of UNION and ORDER BY 错误备忘

    出现这个错误的语句是酱紫的 select xxx from aaa order by xxx union all select yyy from bbb order by yyy 错误原因居然是,如果 ...

随机推荐

  1. javascript 路线整理

    前端开发很重要,编写脚本也不容易. 总结我以前的前端学习经历,基本是一团乱麻:css+javascript是在大三自学的,当时自己做课程设计,逼着自己在一个月之内,写了一个半成品的j2ee网站.当时, ...

  2. Tomcat死机报OutOfMemoryError: PermGen space错误

    最近,用户没怎么使用系统,页面就卡死,访问不了.仔细一看是Tomcat假死,好几次都这样.重启也慢的很,很着急.最后,看了下 conf/logs 里的配置文件,发现是 OutOfMemoryError ...

  3. 如何解决编译linux内核(解决声卡问题),遭遇fatal error: linux/limits.h: 没有那个文件或目录

    最近帮一位上海的朋友搞一块小板,在ubuntu15.04 vivid上已经加载了对应了.ko驱动包 但关键是系统根本就枚举不到该声卡ALC5640,试了OpenSUSE也是一样的结果,看来是内核漏加载 ...

  4. 数学概念——D 期望

    D - 期望 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status ...

  5. UVALive 7148 LRIP 14年上海区域赛K题 树分治

    题意 n个点组成一棵树, 带有点权. 求最长不降的路径的长度, 且路径上最大值最小值之差不超过D. 显然是树分治, 但是分治之后如何维护答案呢. 假设当前重心为g, 分别记录g出发不降路径的长度,以及 ...

  6. SRM 397(1-250pt)

    题意:对于一个长度n的数列(由1-n组成,n <= 8),每次操作可以reverse k个连续的数.问最少多少次操作可以将该数列转化成递增的数列. 解法:就是一个BFS.只是由于最开始学习BFS ...

  7. lightoj 1251 (Two_Sat)

    #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #inclu ...

  8. asp.net mvc vs web form

    译者介绍 小小.NET学童,滴答…滴答…的雨…… 正文如下======================================================= 原文示例(VS2012): 1 ...

  9. Java8特性详解 lambda表达式 Stream

    1.lambda表达式 Java8最值得学习的特性就是Lambda表达式和Stream API,如果有python或者javascript的语言基础,对理解Lambda表达式有很大帮助,因为Java正 ...

  10. sed 批量替换多个文件里的某个字符/串

    提示: 国际惯例使用前先备份 sed -i "s/a/b/g" `grep 'a' -rl ./`