一个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. Spring-Boot中如何使用多线程处理任务

    看到这个标题,相信不少人会感到疑惑,回忆你们自己的场景会发现,在Spring的项目中很少有使用多线程处理任务的,没错,大多数时候我们都是使用Spring MVC开发的web项目,默认的Controll ...

  2. 基于Mina的Http Server以及简单的Http请求客户端

    目的:    Java平台下的内部组件之间的通信.    1.WebService 由于感觉本身Java平台下的Web Service标准就不够统一,相互之间的调用就会有一些问题,更不用说与.net等 ...

  3. elasticsearch.net一个查询问题

    .Query(q => q.Bool(b => b.Must(m => m.MultiMatch(t => t .Fields(f => f.Field(obj => ...

  4. SQL一些记录

    1,2字段约束create unique index [索引名] on 软件信息表(S_SName,S_Edition)

  5. C++仿函数和回调函数的异同

    C++回调函数(callback)与仿函数(functor)的异同 c++仿函数 functor C++仿函数和回调函数的异同

  6. python定时任务模块APScheduler

    一.简单任务 定义一个函数,然后定义一个scheduler类型,添加一个job,然后执行,就可以了 5秒整倍数,就执行这个函数 # coding:utf-8 from apscheduler.sche ...

  7. 牛客多校第六场 J Upgrading Technology dp

    题意: 有n个技能,一开始都是0级,第i个技能从j-1级升到j级,花费$c_{i,j}$,但是花费不一定是正的 所有的技能升到j级时,奖励$d_j$但是奖励也不一定是正的 题解: 用sum[i][j] ...

  8. 为什么程序员都不喜欢使用switch,而是大量的 if……else if ?

    作者:熊爸爸 原文:http://3g.163.com/tech/article/E02RDE6C0511SDDL.html 请用5秒钟的时间查看下面的代码是否存在bug. OK,熟练的程序猿应该已经 ...

  9. day18_文件处理_迭代器_生成器

    #!/usr/bin/env python # -*- coding:utf-8 -*- # ********************day18_文件处理_迭代器_生成器 ************** ...

  10. Spring Cloud Config的配置中心使用非对称性加密

    首先,我们需要通过keytool工具来生成密钥对. keytool是JDK中的一个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认 ...