看板 Database 關於我們 聯絡資訊
※ 引述《arrack (Arrack Tseng)》之銘言: : ※ 引述《lbsjack (^^)》之銘言: : : 大家好: : : 例如: : : 有關預約時段的問題(mysql 5) : : 假設有個人預約的a時段是09:00~11:00 : : 但很不巧的今天維修是b時段10:30~11:30 : : 那在sql中二個區間的日期,要如何判斷是否有交集呢? : : -- : : events的TABLE中有publish_up與publish_down的維修時間, : : 然後會輸入一段開始時間與結束時間與資料庫中的比對,這樣的SQL要怎麼寫?謝謝 : 在新增的時候攔截 : select * from events where : (publist_up between '09:00' and '11:00') or : (publist_down between '09:00' and '11:00') : 如果筆數大於0 就是有交集 : 還是你要問的是另外一種,兩個表格比對的? 我剛測試了一下,已預約時段為08:00~10:00 select * from events where (publish_up >= '2009-07-24 6:00:00' and publish_up <= '2009-07-24 7:00:00') or (publish_down >= '2009-07-24 6:00:00' and publish_down <= '2009-07-24 7:00:00') ↑輸入06:00~07:00 找不到 ,所以可以新增(正確) select * from events where (publish_up >= '2009-07-24 9:00:00' and publish_up <= '2009-07-24 9:10:00') or (publish_down >= '2009-07-24 9:00:00' and publish_down <= '2009-07-24 9:10:00') ↑輸入09:00~09:10,找不到,所以可以新增(錯誤,因為08~10都被預約了) select * from events where (publish_up >= '2009-07-24 10:00:00' and publish_up <= '2009-07-24 10:00:00') or (publish_down >= '2009-07-24 10:00:00' and publish_down <= '2009-07-24 12:00:00') ↑褕入10:00~12:00,找的到,不能新增(錯誤,因為10點視為在10~12,可以新增) 以上這樣子,不知道sql要如何修正,麻煩站上各位,謝謝。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.117.236.157 ※ 編輯: lbsjack 來自: 122.117.236.157 (07/20 21:07)