SQL查询 – 在UNION中使用Order By
在从两个表中提取数据时,如何以编程方式对联合查询进行sorting? 例如,
SELECT table1.field1 FROM table1 ORDER BY table1.field1 UNION SELECT table2.field1 FROM table2 ORDER BY table2.field1
引发exception
注意:这是试图在MS Access Jet数据库引擎上
有时你需要在需要与UNION
组合的每个部分都有ORDER BY
。
在这种情况下
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;
由于两个“select”语句的列名是不同的,因此在ORDER BY子句中的列由它们在结果集中的位置引用是更有利的。
在这个例子中,我们按照supplier_name
/ company_name
的升序对结果进行了sorting,如“ORDER BY 2”所示。
supplier_name
/ company_name
字段位于结果集中的位置#2中。
采取从这里: http : //www.techonthenet.com/sql/union.php
用一个具体的例子:
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
所需的输出:(首先select的结果,即文件夹第一)
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
(使用ALIAS)
这是我见过的最愚蠢的事情,但是有效,你不能和结果争论。
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,第二种模式不起作用,所以这里是我的“解决方法”
在共同的专栏上sorting
这是你可以遇到的最简单的情况。 与许多用户指出的一样,您真正需要做的是在查询结尾添加一个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
在不同的列上sorting
这里是实际上变得棘手的地方。 使用SQL 2012,我尝试了顶端的post,它不工作。
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
在评论的build议之后,我尝试了这个
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
覆盖了Select
语句中build立的Order By
,这使得它不可用。
我能想到的唯一解决scheme就是不使用联合,而是单独运行查询,然后处理它们。 所以基本上,当你想Order By
时,不要使用Union
通过分别使用顺序,每个子集获得顺序,但不是整个集合,这是你想要合并两个表。
你应该使用这样的东西有一个有序的集合:
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
不要抛出exception
如有必要保持内部sorting:
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(Not Checked):
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