【问题标题】:How to use order by in union all如何在 union all 中使用 order by
【发布时间】: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 是的。

标签: mysql sql sql-order-by union


【解决方案1】:

MySQL 期望在每个查询周围加上括号,然后是 order by,如 in the documentation 所述:

要使用ORDER BYLIMIT 子句对整个UNION 结果进行排序或限制,请将各个SELECT 语句括起来,并将ORDER BYLIMIT 放在最后一个语句之后。

所以:

(
    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

注意事项:

  • 不需要在第二个查询中重复列名(第一个查询中定义的名称会在另一个查询中产生)

  • nItemStatenRequestType 看起来像数字,所以应该这样处理(我删除了数字周围的单引号)

【讨论】:

  • 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 的简单示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 2015-04-30
      • 2020-01-18
      • 2010-09-26
      • 1970-01-01
      相关资源
      最近更新 更多