从两个表中提取数据时,如何以编程方式对联合查询进行排序?例如,
SELECT table1.field1 FROM table1 ORDER BY table1.field1
UNION
SELECT table2.field1 FROM table2 ORDER BY table2.field1
抛出异常
注意:这是在 MS Access Jet 数据库引擎上尝试的
从两个表中提取数据时,如何以编程方式对联合查询进行排序?例如,
SELECT table1.field1 FROM table1 ORDER BY table1.field1
UNION
SELECT table2.field1 FROM table2 ORDER BY table2.field1
抛出异常
注意:这是在 MS Access Jet 数据库引擎上尝试的
有时您需要ORDER BY
在需要与UNION
.
在这种情况下
SELECT * FROM
(
SELECT table1.field1 FROM table1 ORDER BY table1.field1
) DUMMY_ALIAS1
UNION ALL
SELECT * FROM
(
SELECT table2.field1 FROM table2 ORDER BY table2.field1
) DUMMY_ALIAS2
SELECT field1 FROM table1
UNION
SELECT field1 FROM table2
ORDER BY field1
我认为这很好地解释了。
以下是使用 ORDER BY 子句的 UNION 查询:
select supplier_id, supplier_name
from suppliers
where supplier_id > 2000
UNION
select company_id, company_name
from companies
where company_id > 1000
ORDER BY 2;
由于两个“选择”语句之间的列名不同,因此在 ORDER BY 子句中通过列在结果集中的位置来引用它们会更有利。
在此示例中,我们按supplier_name
/company_name
升序对结果进行了排序,如“ORDER BY 2”所示。
supplier_name
/company_name
字段在结果集中的位置 #2 。
使用一个具体的例子:
SELECT name FROM Folders ORDER BY name
UNION
SELECT name FROM Files ORDER BY name
文件:
name
=============================
RTS.exe
thiny1.etl
thing2.elt
f.txt
tcpdump_trial_license (1).zip
文件夹:
name
============================
Contacts
Desktop
Downloads
Links
Favorites
My Documents
期望的输出:(首先选择的结果,即文件夹优先)
Contacts
Desktop
Downloads
Favorites
Links
My Documents
f.txt
RTMS.exe
tcpdump_trial_license (1).zip
thiny1.etl
thing2.elt
SQL达到预期的效果:
SELECT name
FROM (
SELECT 1 AS rank, name FROM Folders
UNION
SELECT 2 AS rank, name FROM Files) dt
ORDER BY rank, name
以下是 Northwind 2007 的示例:
SELECT [Product ID], [Order Date], [Company Name], [Transaction], [Quantity]
FROM [Product Orders]
UNION SELECT [Product ID], [Creation Date], [Company Name], [Transaction], [Quantity]
FROM [Product Purchases]
ORDER BY [Order Date] DESC;
在您完成所有联合之后,ORDER BY 子句只需要成为最后一条语句。您可以将多个集合合并在一起,然后在最后一个集合之后放置一个 ORDER BY 子句。
(SELECT table1.field1 FROM table1
UNION
SELECT table2.field1 FROM table2) ORDER BY field1
工作?记住思考集。使用联合获取您想要的集合,然后对其执行操作。
SELECT table1Column1 as col1,table1Column2 as col2
FROM table1
UNION
( SELECT table2Column1 as col1, table1Column2 as col2
FROM table2
)
ORDER BY col1 ASC
SELECT field1
FROM ( SELECT field1 FROM table1
UNION
SELECT field1 FROM table2
) AS TBL
ORDER BY TBL.field1
(使用别名)
这是我见过的最愚蠢的事情,但它确实有效,而且你不能与结果争论。
SELECT *
FROM (
SELECT table1.field1 FROM table1 ORDER BY table1.field1
UNION
SELECT table2.field1 FROM table2 ORDER BY table2.field1
) derivedTable
派生表的内部不会自行执行,但作为派生表工作得很好。我已经在 SS 2000、SS 2005、SS 2008 R2 和所有三个工作上尝试过这个。
这就是它的完成方式
select * from
(select top 100 percent pointx, pointy from point
where pointtype = 1
order by pointy) A
union all
select * from
(select top 100 percent pointx, pointy from point
where pointtype = 2
order by pointy desc) B
浏览这个评论部分,我遇到了两种不同的模式来回答这个问题。可悲的是,对于 SQL 2012,第二种模式不起作用,所以这是我的“解决方法”
这是您可以遇到的最简单的情况。就像许多用户指出的那样,您真正需要做的就是Order By
在查询末尾添加一个
SELECT a FROM table1
UNION
SELECT a FROM table2
ORDER BY field1
或者
SELECT a FROM table1 ORDER BY field1
UNION
SELECT a FROM table2 ORDER BY field1
这就是它实际上变得棘手的地方。使用 SQL 2012,我尝试了顶帖,但它不起作用。
SELECT * FROM
(
SELECT table1.field1 FROM table1 ORDER BY table1.field1
) DUMMY_ALIAS1
UNION ALL
SELECT * FROM
(
SELECT table2.field1 FROM table2 ORDER BY table2.field1
) DUMMY_ALIAS2
按照评论中的建议,我尝试了这个
SELECT * FROM
(
SELECT TOP 100 PERCENT table1.field1 FROM table1 ORDER BY table1.field1
) DUMMY_ALIAS1
UNION ALL
SELECT * FROM
(
SELECT TOP 100 PERCENT table2.field1 FROM table2 ORDER BY table2.field1
) DUMMY_ALIAS2
这段代码确实编译了,但是DUMMY_ALIAS1
并DUMMY_ALIAS2
覆盖Order By
了语句中建立的代码Select
,这使得它无法使用。
我能想到的唯一对我有用的解决方案是不使用联合,而是让查询单独运行然后处理它们。Union
所以基本上,当你想要的时候不要使用Order By
通过单独使用顺序,每个子集都会获得顺序,但不是整个集合,这就是您想要合并两个表的结果。
你应该使用这样的东西来拥有一个有序的集合:
SELECT TOP (100) PERCENT field1, field2, field3, field4, field5 FROM
(SELECT table1.field1, table1.field2, table1.field3, table1.field4, table1.field5 FROM table1
UNION ALL
SELECT table2.field1, table2.field2, table2.field3, table2.field4, table2.field5 FROM table2)
AS unitedTables ORDER BY field5 DESC
第二个表不能在ORDER BY
子句中包含表名。
所以...
SELECT table1.field1 FROM table1 ORDER BY table1.field1
UNION
SELECT table2.field1 FROM table2 ORDER BY field1
不抛出异常
如果需要保持内部排序:
SELECT 1 as type, field1 FROM table1
UNION
SELECT 2 as type, field1 FROM table2
ORDER BY type, field1
(SELECT FIELD1 AS NEWFIELD FROM TABLE1 ORDER BY FIELD1)
UNION
(SELECT FIELD2 FROM TABLE2 ORDER BY FIELD2)
UNION
(SELECT FIELD3 FROM TABLE3 ORDER BY FIELD3) ORDER BY NEWFIELD
试试这个。它对我有用。
对于 Sql Server 2014/2012/Others(未选中):
SELECT * FROM
(
SELECT table1.field1 FROM table1 ORDER BY table1.field1
)
as DUMMY_ALIAS1
UNION ALL
SELECT * FROM
(
SELECT table2.field1 FROM table2 ORDER BY table2.field1
)
as DUMMY_ALIAS2