原文: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. 固定IP和绑定了MAC,可以在设置无线路由器供笔记本电脑和平板上网吗?

    固定IP和绑定了MAC,可以在设置无线路由器供笔记本电脑和平板上网吗? 这跟我们单位一样.很简单:首先要占一个 IP/MAC ,能上外网的,这首先要有,谁要肯给地址,我们这儿领导才有呢.我是网管,当然 ...

  2. HDOJ(HDU) 2164 Rock, Paper, or Scissors?

    Problem Description Rock, Paper, Scissors is a two player game, where each player simultaneously cho ...

  3. UVA 11478 Halum(差分约束)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 [思路] 差分约束系统. 设结点u上的操作和为sum[u] ...

  4. JavaScript中的声明提升

    JavaScript中变量或者函数的声明会被提升(赋值语句不会被提升)到当前函数主体的顶部,不管这个声明是否出现在不可到达的地方. var test = 1; function f() { if(!t ...

  5. L - Oil Deposits

    很清新的一道题,搜索里面最基础的题目,深搜广搜都可以.....不过还是喜欢深搜,写起来简单>.. //////////////////////////////////////////////// ...

  6. Intellij 快捷键大全

    Intellij IDEA的快捷键非常好用,能大大提高我们的开发速度.这里列举了一些常用的快捷键. 快速查找: Ctrl+N  查找类 Ctrl+R  替换当前窗口的文本 Ctrl+F  当前代码中查 ...

  7. Facebook 开源安卓版 React Native,开发者可将相同代码用于网页和 iOS 应用开发

    转自:http://mt.sohu.com/20150915/n421177212.shtml Facebook 创建了React Java 库,这样,Facebook 的工程团队就可以用相同的代码给 ...

  8. Java读书笔记二(封装类)

    1.介绍 都知道java中基本数据类型有非常多,比方string,int--,可是基本数据类型与对象之间是不同的.但非常多情况下,我们希望将基本数据类型当作对象使用,这时候就须要用到封装类. 2.封装 ...

  9. Redis的AOF功能

    引言:  Redis是基于内存的数据库,同时也提供了若干持久化的方案,允许用户把内存中的数据,写入本地文件系统,以备下次重启或者当机之后继续使用.本文将描述如何基于Redis来设置AOF功能 什么是R ...

  10. SpringMVC12拦截器

    创建登陆界面 <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...