看板 Database 關於我們 聯絡資訊
網址: http://rextester.com/WBP97001 SQL: create TABLE myTable_test( type char(1), value int, spec int, time char(6), switch int ); insert into myTable_test select 'A',20,21,'6月20日',0; insert into myTable_test select 'A',22,21,'6月21日',0; insert into myTable_test select 'A',20,21,'6月22日',0; insert into myTable_test select 'A',20,21,'6月22日',0; insert into myTable_test select 'A',22,21,'6月26日',0; insert into myTable_test select 'A',20,21,'6月27日',0; insert into myTable_test select 'B',11,12,'6月13日',0; insert into myTable_test select 'B',11,12,'6月13日',0; insert into myTable_test select 'B',11,12,'6月13日',0; insert into myTable_test select 'B',22,12,'6月15日',0; insert into myTable_test select 'B',13,12,'6月18日',0; insert into myTable_test select 'B',13,12,'6月19日',0; ############## # value > spec update myTable_test set switch = 1 where value > spec; ############ # 1. 同 type # 2. 令 switch = 1 出現的最小時間點為 x # 3. 將 time >= x 的 switch 都設為 1 update myTable_test as t1 inner join ( select type,min(time) as time from myTable_test where switch = 1 group by type ) as t2 on t1.type = t2.type and t1.time >= t2.time set t1.switch = 1; ########## # 搜尋結果 select * from myTable_test; drop table myTable_test; ※ 引述《jord98972005 (舊的)》之銘言: : 資料庫名稱:MySQL : 資料庫版本: : 內容/問題描述: : TYPE Value SPEC Time SWITCH : A 20 21 6月20日 0 : A 22 21 6月21日 1 : A 20 21 6月22日 1 : A 20 21 6月22日 1 : A 22 21 6月26日 1 : A 20 21 6月27日 1 : B 11 12 6月13日 0 : B 11 12 6月13日 0 : B 11 12 6月13日 0 : B 22 12 6月15日 1 : B 13 12 6月18日 1 : B 13 12 6月19日 1 : TABLE如上,Value是量測資料,當value超過安全值(spec)之後,switch切換成1 : 同一type之後時間點的資料都switch=1 : 變到typeB時又重新把switch歸0重新計算 : 想了很久不知道是要用什麼實作,C的話應該兩個if 就能解決了 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.61.233.210 ※ 文章網址: https://www.ptt.cc/bbs/Database/M.1498730786.A.F93.html ※ 編輯: cutekid (42.76.86.234), 06/29/2017 21:55:36