SQL查询-在UNION中使用Order By [英] SQL Query - Using Order By in UNION
本文介绍了SQL查询-在UNION中使用Order By的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从两个表中提取数据时,如何以编程方式对联合查询进行排序?例如,
How can one programmatically sort a union query when pulling data from two tables? For example,
SELECT table1.field1 FROM table1 ORDER BY table1.field1
UNION
SELECT table2.field1 FROM table2 ORDER BY table2.field1
引发异常
注意:这是在MS Access Jet数据库引擎上尝试的
Note: this is being attempted on MS Access Jet database engine
推荐答案
有时,您需要在每个需要与UNION
结合的部分中使用ORDER BY
.
Sometimes you need to have the ORDER BY
in each of the sections that need to be combined with 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
这篇关于SQL查询-在UNION中使用Order By的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文