SQL 合併兩個結果集 乙個倒序,乙個正序

2021-07-03 16:51:51 字數 446 閱讀 3876

要求狀態正常的按end_time 公升序排, 後面跟著 過期的 資料,按end_time 倒序排

select * from (select * from table where status='正常' order by end_time asc) a union all

select * from (select *  from table where status='到期' order by end_time desc)  b

必須外麵包一層,直接兩個結果集 union 發現正常的在上面,到期的在下面,但是各自內部是無序的,也就是說union的時候是沒有order by做的union,

內部的order by沒有生效, 包了一層應該是產生了臨時中間表,次序便固定了。

還有個辦法是用  case when 對 正常的 時間乘以-1 變為負值, 最後order by time  asc便可以得到結果

oracle合併兩個不一樣的結果集

1 通過union 和 union all 合併,但是前提了查出來的結果集要一致 2 如果兩個結果集不一致,就要用到left join on 比如 有a表,我想要求7月和8月的前三天的 都是多少 select from a 月份 month 日期 day price 071 1600072 1207...

解決UNION ALL合併兩個結果集後排序的問題

日常開發中,如果實用union all合併兩個已經排好序的結果集的時候,需求是第二個結果集資料排在第乙個結果集資料下面,單純的實用order by是無效的,因為order by的優先順序比union all低。例如 select one.from select t1.from table1 t1 w...

mysql 合併兩個查詢結果

顯然,查詢結果的time為主鍵,目的是將兩個查詢結果合成乙個結果。如果在 中實現,一次便利乙個表新增到另外乙個表中,效率非常低。那麼在mysql 中實現方式為 出金 withdraw select from select date complete time as time,sum amt as a...