UNION All中ORDER By的使用
一个sql中,union了几个子查询。单独执行每个子查询都没问题,但union后执行,报
ORA-00904: "xxx": invalid identifier
关于union的使用:
SQL: UNION Query:
http://www.techonthenet.com/sql/union.php
SQL: UNION ALL Query:
http://www.techonthenet.com/sql/union_all.php
所union的各个子查询要有相同数量的列,且对应位置的列必须具有相同的数据类型;但列的名字可以不同。
the diffrence between UNION ALL and UNION is that UNION will attempt to eliminate duplicates.
关于order by的使用:
SQL: ORDER BY Clause
http://www.techonthenet.com/sql/order_by.php
Example #3
You can also sort by relative position in the result set, where the first field in the result set is 1. The next field is 2, and so on.
This would return all records sorted by the supplier_city field in descending order, since the supplier_city field is in position #1 in the result set.
union中order by的使用:
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.
所以,只能在union的最后一个子查询中使用order by,而这个order by是针对整个unioning后的结果集的。So:
如果unoin的几个子查询列名不同,如
这里的问号如果是company_name,则执行整个查询会报“company_name:invalid identifier”(当然,单独执行第二个含order by的子查询是没有问题的);这是因为unioning后结果集的列名是以第一个参加union的子查询的列名为准的;order by针对的是整个unioning后的结果集。对整个查询结果来说,无”company_name“这个字段
如果是supplier_name,则单独执行第二个含order by的子查询是会报“supplier_name:invalid identifier”的,而执行整个查询是没有问题的,因为order by针对的是unioning后的整个结果集,而这“整个结果集”是有supplier_name这列的(以第一个union子查询的列名作为unioning后整个结果集的列名)
为了避免这样事情的发生,可以:
1 使用列序号代替实际列名。如:
2 为unoin的各个子查询使用相同的列名,如:
这样,不管是执行整个查询还是单独执行包含order by的最后一个union子查询,都不会有问题。
UNION All中ORDER By的使用的更多相关文章
- SQL 语句中union all和order by同时使用
最近做的一个财物管理系统中查询过期或逾期的存储过程,返回 “财物所属的案件名称”,“财物名称”,“财物编号”,“过期或逾期时间”(超期或逾期前7天开始预警). 遇到“union all ...
- UNION 查询中的排序
MSSQL 不允许在UNION查询中使用 ORDER BY 因此,当我们需要这种功能的时候,就需要绕一些弯路. 比如有一张学生表student 和教师表 teacher , 我们要查询所有的教师学生的 ...
- MySQL中order by中关于NULL值的排序问题
MySQL中order by 排序遇到NULL值的问题 MySQL数据库,在order by排序的时候,如果存在NULL值,那么NULL是最小的,ASC正序排序的话,NULL值是在最前面的. 如果我们 ...
- mysql 中order by 与group by的顺序
mysql 中order by 与group by的顺序 是: select from where group by order by 注意:group by 比order by先执行,order b ...
- ORACLE中order by造成分页不正确原因分析
工作中遇到的问题: 为调用方提供一个分页接口时,调用方一直反应有部分数据取不到,且取到的数据有重复的内容,于是我按以下步骤排查了下错误. 1.检查分页页码生成规则是否正确. 2.检查SQL语句是否正 ...
- hive 中 Order by, Sort by ,Dristribute by,Cluster By 的作用和用法
order by order by 会对输入做全局排序,因此只有一个reducer(多个reducer无法保证全局有序) 只有一个reducer,会导致当输入规模较大时,需要较长的计算时间. set ...
- hive中order by、distribute by、sort by和cluster by的区别和联系
hive中order by.distribute by.sort by和cluster by的区别和联系 order by order by 会对数据进行全局排序,和oracle和mysql等数据库中 ...
- sql中order by和group by的区别
order by 和 group by 的区别: 1,order by 从英文里理解就是行的排序方式,默认的为升序. order by 后面必须列出排序的字段名,可以是多个字段名. 2,group b ...
- MySql中order by和union all同时使用
() UNION ALL () 两边的语句加上括号就可以了
随机推荐
- jquery.js和jquery.min.js的区别和springboot整合echarts.min.js
1.区别:jquery官网提供2种jQuery的下载,一种是jquery.js另一种是jquery.min.js文件名不一定完全相同,但通常情况下:jquery.js是完整的未压缩的jQuery库,文 ...
- 安装keepalived 出现configure: error: Popt libraries is required
keepalived执行./configure --prefix=/usr/local/keepalived时报错:configure: error: Popt libraries is requir ...
- 同步图计算实现pageRank算法
pageRank算法是Google对网页重要性的打分算法. 一个用户浏览一个网页时,有85%的可能性点击网页中的超链接,有15%的可能性转向任意的网页.pageRank算法就是模拟这种行为. Rv:定 ...
- MongoDB使用固定集合
MongoDB中的固定集合:大小是固定的,类似于循环队列,如果没有空间了,最老的文档会被删除以释放空间,新插入的会占据这块空间. 1.固定集合(oplog) oplog是一个典型的固定集合,因为其大小 ...
- RAksmart服务器具备哪些特点?
美国RAKsmart机房运营多年,前身是提供美国军用服务器业务,拥有着庞大的用户群体和消费者,那RAksmart服务器具备哪些特点呢? 1.美国raksmart服务器特点——硬盘超大 美国raksma ...
- jmeter遇到的问题:java.net.ConnectException: Connection refused: connect
1.使用jmeter执行脚本,在察看结果树的的响应数据中看到的错误提示如下: java.net.ConnectException: Connection refused: connect at jav ...
- leetcode-50-pow()
题目描述: 方法一: class Solution: def myPow(self, x: float, n: int) -> float: if n<0: x = 1/x return ...
- java实现单链表增删改查
package 数据结构算法.链表; /* *定义节点 * 链表由节点构成 */ public class Node<E> { private E e; //数据data private ...
- jenkins实现不同角色查看不同视图
1.安装插件Role-based Authorization Strategy 2.开启插件 系统管理>>>全局安全配置 3.创建角色和用户 4.登陆查看,只能看到travel开头的 ...
- C++——运算符重载
运算符重载编程基础 例如: //全局函数 完成 +操作符 重载 Complex operator+(Complex &c1, Complex &c2) //类成员函数 完成 -操作符 ...