SQL查询-在UNION中使用Order By

从两个表中提取数据时,如何以编程方式对联合查询进行排序?例如,


SELECT table1.field1 FROM table1 ORDER BY table1.field1

UNION

SELECT table2.field1 FROM table2 ORDER BY table2.field1

引发异常


注意:正在MS Access Jet数据库引擎上尝试这样做


拉莫斯之舞
浏览 1679回答 3
3回答

素胚勾勒不出你

有时您需要ORDER BY在每个部分中将与组合在一起UNION。在这种情况下SELECT * FROM (  SELECT table1.field1 FROM table1 ORDER BY table1.field1) DUMMY_ALIAS1UNION ALLSELECT * FROM(   SELECT table2.field1 FROM table2 ORDER BY table2.field1) DUMMY_ALIAS2

四季花海

SELECT field1 FROM table1UNIONSELECT field1 FROM table2ORDER BY field1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

SQL Server