一个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子查询,都不会有问题。

最新文章

  1. IOS tableview下拉刷新上拉加载分页
  2. kbengine0.4.20源代码分析(一)
  3. [CareerCup] 11.7 Tower of People in Circus 马戏团的人塔
  4. 前端开发 Grunt 之 Connect
  5. KVM/QEMU桥接网络设置及kvm资料
  6. htm5 user-scalable 的意思
  7. Asp.Net HttpApplication请求管道与Session(一)
  8. Java Timer触发定时器
  9. Linux08--Shell程序设计03 shell script
  10. 如何实现一个 Virtual DOM 及源码分析
  11. hackerrank Alex对战Fedor
  12. Solr简单使用
  13. Oracle根据时间恢复已删除提交的数据
  14. Flex-box入门---flex-grow, flex-shrink, flex-basis
  15. 在docker集群下,使用VNC,物理机器重启后VNC失败解决
  16. JS判断是电脑浏览器还是手机端浏览器,并根据不同的终端跳转到不同的网址
  17. Android Studio启动后出现cannot bind to 127.0.0.1:5037 10048的解决办法
  18. SimpleAdapter & BaseAdapter
  19. EF CodeFirst 数据库的操作
  20. Python3 使用 matplotlib 画折线图

热门文章

  1. D3.js(v3)+react 制作 一个带坐标与比例尺的柱形图 (V3版本)
  2. MySQL 07章_子查询
  3. (转)Google Protocol Buffer 的使用和原理
  4. indexof方法区分大小写
  5. [JZOJ6341] 【NOIP2019模拟2019.9.4】C
  6. BZOJ 1010 (HNOI 2008) 玩具装箱
  7. SpringBoot_03_SpringBoot对其他技术的整合
  8. linux操作练习题
  9. System.Web.Mvc.Controller.cs
  10. Vue-Grid-Layout分享一款好用的可拖拽组件