对 union 结果进行排序


# 第一种排序      
  select msgId ,body ,[from] ,[to] ,timetag ,attach ,convType ,type as msgType
        from MessageRecord 
        where [from] = 'aaa' and [to] = 'bbb'  and timetag < '1625220012990'
        UNION
        select msgId ,body ,[from] ,[to] ,timetag ,attach ,convType ,type as msgType
        from MessageRecord
        where [to] = 'bbb' and [from] = 'aaa' and timetag < '1625220012990'
        order by timetag DESC;
                
# 第二种排序
    select  row_number() over (order by timetag DESC) row_num, * FROM
    (
                (select msgId ,body ,[from] ,[to] ,timetag ,attach ,convType ,type as msgType
                    from MessageRecord 
                    where [from] = 'aaa' and [to] = 'bbb'  and timetag < '1625220012990'
                )
                    UNION
                (select msgId ,body ,[from] ,[to] ,timetag ,attach ,convType ,type as msgType
                    from MessageRecord
                    where [to] = 'bbb' and [from] = 'aaa' and timetag < '1625220012990'
                )
    )temp;

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM