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
ORA-00933 UNION 与 ORDER BY的更多相关文章
- 让UNION与ORDER BY并存于SQL语句当中
在SQL语句中,UNION关键字多用来将并列的多组查询结果(表)合并成一个结果(表),简单实例如下: SELECT [Id],[Name],[Comment] FROM [Product1] UNIO ...
- mysql 错误 1221 Incorrect usage of union and order by
今天有个项目出现了问题 问题原因是union和order by 的问题.关于这个问题的解决方案可以猛击下面的链接. http://blog.csdn.net/gtuu0123/article/deta ...
- mysql中的union和order by、limit
我有一个表 CREATE TABLE `test1` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) N ...
- MySQL中union和order by一起使用的方法
MySQL中union和order by是可以一起使用的,但是在使用中需要注意一些小问题,下面通过例子来说明.首先看下面的t1表. 1.如果直接用如下sql语句是会报错:Incorrect usage ...
- 同时使用Union和Order by问题(ORA-00933错误)解决
之前,同事在编写视图的过程中遇到这样了这个错误.我把简化后的语句整理如下: 1: select 2: '2016' as nf, 3: qxdm, 4: round(sum(tbdlmj)/10000 ...
- MySql union与order by
[MySql union与order by] 如果您想使用ORDER BY或LIMIT子句来对全部UNION结果进行分类或限制,则应对单个地SELECT语句加圆括号,并把ORDER BY或LIMIT放 ...
- MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)
我在一个业务中采用了按月的分表策略,当查询的条件跨月的时候,使用了union all汇总2个表的数据,并按插入时间倒序排列.查询并不复杂,但是当执行的时候却报错了. SELECT * FROM `ta ...
- union 和order by 使用时排序不正确
静态专题和APP版专题(order by不起作用): [query] sql=(select sp_f13577,sp_f13576 from sp_t113 where url_1 not like ...
- mysql Incorrect usage of UNION and ORDER BY 错误备忘
出现这个错误的语句是酱紫的 select xxx from aaa order by xxx union all select yyy from bbb order by yyy 错误原因居然是,如果 ...
随机推荐
- LightOJ 1422 Halloween Costumes(记忆化搜索)
题意:给你n天分别要穿的衣服,可以套着穿,但是一旦脱下来就不能再穿了,问这n天要准备几件衣服. =============================================== ...
- linux下的webserver BOA及CGIC库的使用指南(转帖)
我把网页挂载到nfs 下面的文件中(需要新建一个文件www ),不过这样很方便! 安装过程 ====================================================== ...
- 【动态规划】【二分】【最长上升子序列】Vijos P1028 魔族密码
题目链接: https://vijos.org/p/1028 题目大意: 给N个字符串(N<=2000),求能组成词链的单词最多有几个. 如果在一个由一个词或多个词组成的表中,除了最后一个以外, ...
- Merge Intervals——LeetCode
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- openvpn服务器端配置文件
- Spring 3.x企业应用开发实战(9-1)----依赖注入
Spring中的依赖注入方式:属性注入.构造函数注入和工厂方式注入. 1.属性注入 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象. 属性注入要求Bean提供一个默认的构造函数,在J ...
- Linux下设置允许myql数据库远程连接
系统环境: Linux ubuntu --generic #-Ubuntu SMP Thu Apr :: UTC x86_64 x86_64 x86_64 GNU/Linux 系统中已经安装好mysq ...
- FC和SCSI
IDE(Integrated Drive Electronics)即"电子集成驱动器",它的本意是指把"硬盘控制器"与"盘体"集成在一起的硬 ...
- codeforces 610B
Description Vika has n jars with paints of distinct colors. All the jars are numbered from 1 to n an ...
- 根据类名查所属jar包
在开发时经常会参考一些现有的例子,但有些例子只有代码,代码中引用的类所属的jar包却没有明确说明,如何找到一个类所属的jar包,可以通过访问以下网址解决:http://www.findjar.com