MySQL判斷時(shí)間段是否重合的兩種方法
發(fā)布日期:2022-07-15 19:08 | 文章來源:源碼之家
兩種寫法。如圖,4種重合情況和2種不重合情況。
第一種寫法:
-- 時(shí)間段 a,b SELECT * FROM table WHERE (start_time >= a and end_time <= b) -- 被包含了 or (end_time >= a and end_time <=b) or (start_time >= a and start_time <=b) or (start_time <= a and end_time >=b)
解析:where后的4個(gè)條件分別代表了圖中4種重合的情況。
但是第一種情況被2和3包含了,所以簡(jiǎn)化一下寫法:
SELECT * FROM table WHERE (end_time >= a and end_time <=b) or (start_time >= a and start_time <=b) or (start_time <= a and end_time >=b);
第二種寫法:
SELECT * FROM table WHERE not (start_time > b or end_time < a);
到此這篇關(guān)于MySQL判斷時(shí)間段是否重合的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章