iT邦幫忙

0

SQL 兩個不同時間的排序問題

sql

請問我資料庫的設計方式簡略如下:
id aa_time bb_time
1 2010-01-01 10:44:49 0000-00-00 00:00:00
2 0000-00-00 00:00:00 2010-01-13 10:45:06
3 0000-00-00 00:00:00 2010-01-12 10:45:11
4 2010-01-11 10:45:29 0000-00-00 00:00:00
請教我該如何使用 order by 的方式 讓 aa_time 和 bb_time 進行 desc 排序呢
使之排序結果為
2010-01-13 10:45:06
2010-01-12 10:45:11
2010-01-11 10:45:29
2010-01-01 10:44:49

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

20
jamesjan
iT邦高手 1 級 ‧ 2010-01-14 12:50:55
最佳解答

如果您的資料的規則是如同您所記錄的
aa_time 與 bb_time 只有擇一有時間
則 wiselou 大的方式即可

如果不是那您可能需要利用到 UNION 的方式

<pre class="c" name="code">select * from (
select id,aa_time as newtime from table where aa_time<>'0000-00-00 00:00:00'
union
select id,bb_time as newtime from table where bb_time<>'0000-00-00 00:00:00'
) t order by t.newtime
16
外獅佬
iT邦大師 1 級 ‧ 2010-01-14 12:16:28

試試這個方法:

<pre class="c" name="code">SELECT * FROM (SELECT CASE WHEN(aa_time='0000-00-00 00:00:00') THEN bb_time ELSE aa_time END AS newtime FROM yourtable) AS newtable ORDER BY newtime DESC

我要發表回答

立即登入回答