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 错误原因居然是,如果 ...
随机推荐
- 【转】如何开发苹果iOS操作平台下的应用程序?
原文网址:http://zhidao.baidu.com/link?url=vxRWjCchSstFmVKvxEqLqfqomu2h5kF-NLAIVEehQgN_FnYtEi4f5yPMS6ywbU ...
- Partition List ——LeetCode
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- Maximum Product Subarray——LeetCode
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Construct Binary Tree from Preorder and Inorder Traversal——LeetCode
Given preorder and inorder traversal of a tree, construct the binary tree. 题目大意:给定一个二叉树的前序和中序序列,构建出这 ...
- JavaScript 调试常见报错以及修复方法
(看到一篇调试JS很有用的文章,收藏一下) JavaScript 调试是一场噩梦:首先给出的错误非常难以理解,其次给出的行号不总有帮助.有个查找错误含义,及修复措施的列表,是不是很有用? 以下是奇怪的 ...
- hdu 1754 线段树模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 #include <cstdio> #include <cmath> # ...
- 完整的站内搜索Demo(Lucene.Net+盘古分词)
前言 首先自问自答几个问题,以让各位看官了解写此文的目的 什么是站内搜索?与一般搜索的区别? 很多网站都有搜索功能,很多都是用SQL语句的Like实现的,但是Like无法做到模糊匹配(例如我搜索“.n ...
- redis+keeplived分布式缓存
redis(三)redis+Keepalived主从热备秒级切换 博客分类: 分布式缓存Redis redis高可用Keepalived 一 简介 安装使用centos 5.10 Master 19 ...
- Meth | phpstorm 2016.2 的最新破解方法(截止2016-8-1)
今天刚更新了phpstorm 2016.2版本,发现网上提供的破解地址都有问题,即*.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀. 最后网上找到一个 ...
- 使用 Java 配置进行 Spring bean 管理--转
概述 众所周知,Spring 框架是控制反转 (IOC) 或依赖性注入 (DI) 模式的推动因素,而这种推动是通过基于容器的配置实现的.过去,Spring 允许开发人员使用基于 XML 的配置,通过利 ...