SQL 查询 - 在 UNION 中使用 Order By [英] SQL Query - Using Order By in UNION

查看:36
本文介绍了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

推荐答案

有时需要在需要与UNIONORDER 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆