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 错误原因居然是,如果 ...
随机推荐
- Keepalived实现Redis Failover
一.环境说明 操作系统版本:RHEL 5.4_64 redis版本:2.8.17 keepalived版本:1.1.15 master:10.142.130.81 slave: 10.142.130 ...
- 图论(网络流):[CTSC2001]终极情报网
[CTSC2001]终极情报网 [题目描述] 在最后的诺曼底登陆战开始之前,盟军与德军的情报部门围绕着最终的登陆地点展开了一场规模空前的情报战. 这场情报战中,盟军的战术是利用那些潜伏在敌军内部的双重 ...
- 【模拟】NEERC15 A Adjustment Office (2015-2016 ACM-ICPC)(Codeforces GYM 100851)
题目链接: http://codeforces.com/gym/100851 题目大意: 一个N*N的矩阵A,Ai,j=i+j,Q次操作,每次分两种,R r取出第r行还未被取的所有数,并输出和.C c ...
- eucalyptus的网络模式
总共有四种网络模式,默认采用的是system模式 SYSTEM Mode 最简单的网络配置.Eucalyptus分配mac地址,使用 Xen Bridge,配合已有的 DHCP DHCP 來分配 IP ...
- F - Wormholes
题目大意: 农民约翰在农场散步的时候发现农场有大量的虫洞,这些虫洞是非常特别的因为它们都是单向通道,为了方便现在把约翰的农田划分成N快区域,M条道路,W的虫洞. 约翰是时空旅行的粉丝,他希望这样做,在 ...
- OpenERP里面继承的用法
最近开发遇到了这样的问题:需要往HR模块里面添加一些查询条件,这些查询条件是HR模型里已经写好的,直接修改HR肯定可以实现,但是HR模块一旦修改就会导致一系列的错误,OE开发中的一项基本原则就是不可修 ...
- bzoj2049: [Sdoi2008]Cave 洞穴勘测
lct入门题? 得换根了吧TAT 这大概不是很成熟的版本.. #include<iostream> #include<cstring> #include<cstdlib& ...
- PHP学习之[第08讲]数据库MySQL基础之增删改查
一.工具: 1.phpMyAdmin (http://www.phpmyadmin.net/) 2.Navicat (http://www.navicat.com/) 3.MySQL GUI Tool ...
- iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath(汇总)
iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath 首先分析有几种原因,以及相应的解决方法 1.UITableViewCell的userInterac ...
- android scrollview组件禁止滑动的方法
xml配置: android:id="@+id/sc_freement" android:layout_width="fill ...