一个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.

Sql代码 
SELECT supplier_city   
FROM suppliers   
WHERE supplier_name = 'IBM'  
ORDER BY 1 DESC;  

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的几个子查询列名不同,如

Sql代码 
select supplier_id, supplier_name   
from suppliers   
UNION  
select company_id, company_name   
from companies   
ORDER BY ?;  

这里的问号如果是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 使用列序号代替实际列名。如:

Sql代码 
select supplier_id, supplier_name   
from suppliers   
UNION  
select company_id, company_name   
from companies   
ORDER BY 2;  

2 为unoin的各个子查询使用相同的列名,如:

Sql代码:
select supplier_id as id, supplier_name as name  
from suppliers   
UNION  
select company_id as id, company_name as name  
from companies   
ORDER BY name;  

这样,不管是执行整个查询还是单独执行包含order by的最后一个union子查询,都不会有问题。

UNION All中ORDER By的使用的更多相关文章

  1. SQL 语句中union all和order by同时使用

            最近做的一个财物管理系统中查询过期或逾期的存储过程,返回 “财物所属的案件名称”,“财物名称”,“财物编号”,“过期或逾期时间”(超期或逾期前7天开始预警). 遇到“union all ...

  2. UNION 查询中的排序

    MSSQL 不允许在UNION查询中使用 ORDER BY 因此,当我们需要这种功能的时候,就需要绕一些弯路. 比如有一张学生表student 和教师表 teacher , 我们要查询所有的教师学生的 ...

  3. MySQL中order by中关于NULL值的排序问题

    MySQL中order by 排序遇到NULL值的问题 MySQL数据库,在order by排序的时候,如果存在NULL值,那么NULL是最小的,ASC正序排序的话,NULL值是在最前面的. 如果我们 ...

  4. mysql 中order by 与group by的顺序

    mysql 中order by 与group by的顺序 是: select from where group by order by 注意:group by 比order by先执行,order b ...

  5. ORACLE中order by造成分页不正确原因分析

     工作中遇到的问题: 为调用方提供一个分页接口时,调用方一直反应有部分数据取不到,且取到的数据有重复的内容,于是我按以下步骤排查了下错误. 1.检查分页页码生成规则是否正确. 2.检查SQL语句是否正 ...

  6. hive 中 Order by, Sort by ,Dristribute by,Cluster By 的作用和用法

    order by order by 会对输入做全局排序,因此只有一个reducer(多个reducer无法保证全局有序) 只有一个reducer,会导致当输入规模较大时,需要较长的计算时间. set ...

  7. hive中order by、distribute by、sort by和cluster by的区别和联系

    hive中order by.distribute by.sort by和cluster by的区别和联系 order by order by 会对数据进行全局排序,和oracle和mysql等数据库中 ...

  8. sql中order by和group by的区别

    order by 和 group by 的区别: 1,order by 从英文里理解就是行的排序方式,默认的为升序. order by 后面必须列出排序的字段名,可以是多个字段名. 2,group b ...

  9. MySql中order by和union all同时使用

    () UNION ALL () 两边的语句加上括号就可以了

随机推荐

  1. axios 基本运用

    axios是专门对ajax请求进行封装的一个插件,其返回一个promise对象,用法跟ES6的promise很相似 一.安装axios插件npm install axios 二.引入axios插件 在 ...

  2. java_函数式编程写法

    package cn.aikang.Test; import org.junit.Test; import java.util.Scanner; import java.util.function.S ...

  3. redis 体系结构

    程序strings   key-value 类型 ,value不仅是String,也可以是数字.使用strings 类型可以完全实现目前 Memcache 的功能,并且效率更高,还可以享受redis的 ...

  4. linux 服务器安装mysql5.6

    1.移除CentOS默认的mysql-libs: whereis mysql 2.为了避免冲突,先移除CenttOS上默认的mysql-libs: yum remove mysql-libs 3.然后 ...

  5. 跟我一起使用Vue.js + Docker 部署项目

    本文学习自:https://juejin.im/post/5bee5ddde51d457b8a33938c 项目环境是在ubuntu下,记得要在root目录下,不然安装vue会报错 npm insta ...

  6. Java怎样实现解析身份证号

    身份证号解析,demo /** * 身份证号解析demo * */ public class TestArea { public static void main(String[] args) { S ...

  7. XML、JSON、ProtocolBuffer特点比较

    XML JSON PB Lua 数据结构支持 复杂结构 简单结构 较复杂结构 复杂结构 数据保存方式 文本 文本 二进制 文本 数据保存大小 大 一般 小 一般 解析效率 慢 一般 快 稍快 语言支持 ...

  8. 解决方案 -SQL脚本建表产生ORA-00942错误

    一.问题简介 1.开发环境 操作系统:win10 数 据 库:Oracle11g 数据库连接工具:Navicat  Premium 2.问题简述 在使用SQL Development.Navicat  ...

  9. SSH整合时执行hibernate查询报错:java.lang.ClassCastException: com.ch.hibernate.Depart

    今天在整合ssh三个框架时,有一个功能,是查询所有员工信息,且员工表和部门表是多对一的映射关系,代码能正常运行到查询得到一个List集合,但在页面展示的时候,就报异常了, java.lang.Clas ...

  10. floyd类型题UVa-10099-The Tourist Guide +Frogger POJ - 2253

    The Tourist Guide Mr. G. works as a tourist guide. His current assignment is to take some tourists f ...