Re: [SQL ] 請問查詢資料區間的語法

看板Database (資料庫)作者 (KID)時間13年前 (2013/02/02 02:03), 編輯推噓2(204)
留言6則, 4人參與, 最新討論串2/3 (看更多)
select a.type,a.idmin,min(b.idmax) from ( select type,id as idmin from table t1 where not exists( select id from table t2 where t2.id = t1.id - 1 and t2.type = t1.type ) ) a,( select type,id as idmax from table t3 where not exists( select id from table t4 where t4.id = t3.id +1 and t4.type = t3.type ) ) b where a.type = b.type and a.idmin <= b.idmax group by a.type,a.idmin ※ 引述《cherman (penguin)》之銘言: : 請問我有一個資料表 : ID TYPE : ================== : 1 A : 2 A : 3 B : 4 B : 5 A : 6 C : 希望查詢的結果是: : TYPE IDMIN IDMAX : ===================== : A 1 2 : B 3 4 : A 5 5 : C 6 6 : 我的語法應該怎麼下才有上列結果?謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 36.225.168.230

02/03 14:15, , 1F
這樣的寫subquery,DB會跑的唉唉叫
02/03 14:15, 1F

02/03 16:45, , 2F
哈哈 ^_^
02/03 16:45, 2F

02/03 19:50, , 3F
拐拐叫
02/03 19:50, 3F

02/03 19:59, , 4F
身為DBA寫出這種query的developer都想跟他好好談談…
02/03 19:59, 4F

02/03 20:22, , 5F
(逃)
02/03 20:22, 5F

02/05 10:25, , 6F
CUTE大太厲害了,感恩…
02/05 10:25, 6F
文章代碼(AID): #1H30DX0B (Database)
討論串 (同標題文章)
文章代碼(AID): #1H30DX0B (Database)