如何在 union all 中使用 order by
Posted
技术标签:
【中文标题】如何在 union all 中使用 order by【英文标题】:How to use order by in union all 【发布时间】:2020-03-20 10:27:57 【问题描述】:我有 2 个表,我使用 union all 来合并这两个表。 我有 strCreatedOn 列作为时间戳。现在我无法在其中使用 order by 子句
select nVendorId,
'bank'as change_column,
nRequestType as change_type,
strCreatedOn as Timestamp,
nItemState as status
from vaamoz_admin.tblbankdetails
where nItemState = '1' AND nRequestType='4'
union all
select nVendorId,
'kyc' as change_column,
nRequestType as change_type ,
strCreatedOn as Timestamp,
nItemState as status
from vaamoz_admin.tblkycdetails
where nItemState = '1' AND nRequestType='4'
【问题讨论】:
用您正在使用的数据库标记您的问题。 @GordonLinoff 是的。 【参考方案1】:mysql 期望在每个查询周围加上括号,然后是 order by
,如 in the documentation 所述:
要使用
ORDER BY
或LIMIT
子句对整个UNION
结果进行排序或限制,请将各个SELECT
语句括起来,并将ORDER BY
或LIMIT
放在最后一个语句之后。
所以:
(
select
nVendorId,
'bank'as change_column,
nRequestType as change_type,
strCreatedOn as Timestamp,
nItemState as status
from vaamoz_admin.tblbankdetails
where nItemState = 1 and nRequestType = 4
) union all (
select
nVendorId,
'kyc',
nRequestType,
strCreatedOn,
nItemState
from vaamoz_admin.tblkycdetails
where nItemState = 1 AND nRequestType = 4
)
order by Timestamp
注意事项:
不需要在第二个查询中重复列名(第一个查询中定义的名称会在另一个查询中产生)
nItemState
和 nRequestType
看起来像数字,所以应该这样处理(我删除了数字周围的单引号)
【讨论】:
MySQL 期望每个查询都带有括号 不。在这种情况下,单独查询的括号是多余的(但仍然可以使用它们)。它们必须仅在您想将排序(和限制)应用于单独查询而不是组合行集时使用。 @Akina:是的。问题是关于(外部)排序依据,需要括号。 没有。请阅读示例下方的句子。 不带括号的语句等同于刚才显示的带括号的语句。 我的评论字体中用斜体写的文字直接引用自参考手册,通过您的答案中提供的链接。您可以通过网页搜索来验证这一点。【参考方案2】: select t.* from
(
select nVendorId,
'bank'as change_column,
nRequestType as change_type,
strCreatedOn as Timestamp,
nItemState as status
from vaamoz_admin.tblbankdetails
where nItemState = '1' AND nRequestType='4'
union all
select nVendorId,
'kyc' as change_column,
nRequestType as change_type ,
strCreatedOn as Timestamp,
nItemState as status
from vaamoz_admin.tblkycdetails
where nItemState = '1' AND nRequestType='4'
) as t order by t.Timestamp
【讨论】:
【参考方案3】:在大多数数据库中,您只需添加一个order by
:
select nVendorId, 'bank'as change_column, nRequestType as change_type,
strCreatedOn as Timestamp, nItemState as status
from vaamoz_admin.tblbankdetails
where nItemState = '1' AND nRequestType='4'
union all
select nVendorId, 'kyc' as change_column, nRequestType as change_type ,
strCreatedOn as Timestamp, nItemState as status
from vaamoz_admin.tblkycdetails
where nItemState = '1' AND nRequestType='4'
order by Timestamp;
在某些情况下,您需要一个子查询:
select t.*
from ((select nVendorId, 'bank'as change_column, nRequestType as change_type,
strCreatedOn as Timestamp, nItemState as status
from vaamoz_admin.tblbankdetails
where nItemState = '1' AND nRequestType='4'
) union all
(select nVendorId, 'kyc' as change_column, nRequestType as change_type ,
strCreatedOn as Timestamp, nItemState as status
from vaamoz_admin.tblkycdetails
where nItemState = '1' AND nRequestType='4'
)
) t
order by Timestamp;
Here 是一个使用 MySQL 的简单示例。
【讨论】:
以上是关于如何在 union all 中使用 order by的主要内容,如果未能解决你的问题,请参考以下文章
在android中使用UNION ALL时如何对特定语句使用order by
如何使用 SQLite 使用 UNION ALL 和 ORDER BY 进行排序