遇到的問題:
一個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的幾個子查詢列名不同,如
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 使用列序號代替實際列名。如:
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子查詢,都不會有問題。
Q&A:
http://p2p.wrox.com/sql-language/9505-order-union.html
Q:
引用
I have two tables, TableA and TableB defined as follows,
TableA
A1 int
A2 int
A3 int
TableB
B1 int
B2 int
B3 int
If I try to run this query, SQL Server says syntex failed at the Order By clouse. Is such Order by not allowed in SQL, Any other way to achieve this?
Sql代碼
(Select A1, A2 from TableA)
Union All
(Select B1, B2 from TableB Order by B3)
Any help will be appreciated.
A:
引用
First of all, you can not order by a column that is not included in your SELECT list(我注:這句話是錯誤的;可以order by一個不在select列表中的column). Secondly, when performing a UNION query the ORDER BY clause must be(我注:not “must be”!) a column index not a column name, because a UNION query does not have column headings (although SQL Server(我注:此處泛指DBMS) pretends that it has by picking the column names used in the first query although this is not ANSI compliant]). Assuming you want to order the second column (A2 and B2) your query should look like this:
Code:
Sql代碼
SELECT A1, A2
FROM TableA
UNION ALL
SELECT B1, B2
FROM TableB
ORDER BY 2
Conceptually, ORDER BY works by producing the final query table with all the queries joined together (if it is a UNION query), then it orders the query results and does not care about what is in the database.