Re: [SQL ] 請教這個問題如何下SQL呢?

看板Database作者 (YLK)時間18年前 (2006/06/25 15:42), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串9/13 (看更多)
※ 引述《weisor (無所事事)》之銘言: : ※ 引述《ylk (YLK)》之銘言: : : id used time : : --------- ------- -------- : : A 5 13:30 : : B 5 13:00 : : C 4 14:00 : : D 1 12:00 : : E 0 11:00 : : 各位前輩好,想請教一下一個SQL問題。 : : 需求:找到 time 時間最早的資料,不過,如果 used 有 > 0 的,要優先選擇。 : : 以上列資料表來看,我要選擇的是 資料D。 : : 想了很久,還是想不出來這個 SQL 要怎麼下,請問有人能幫忙提醒一下嗎? : : 謝謝! : 只取一筆 : select * from T : order by case when (used > 0) then 1 else 0 end desc, time asc : limit 1 : 噁心的寫法 : select * from T : join ( : select case when used > 0 then 1 else 0 end as u, min(time)as t from T : group by case when used > 0 then 1 else 0 end) x : on x.u = case when used > 0 then 1 else 0 end and x.t = T.time : where (exists(select * from T where used > 0) and used > 0) : or (not exists(select * from T where used > 0) and used = 0) 謝謝您的指導。 想請問您,您的環境是? case 這個欄位代表的意思是? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.224.29.198
文章代碼(AID): #14dZviVD (Database)
討論串 (同標題文章)
本文引述了以下文章的的內容:
以下文章回應了本文
完整討論串 (本文為第 9 之 13 篇):
文章代碼(AID): #14dZviVD (Database)