慕的地10843
2019-07-13 18:54:50
在MySQL中,如果我有一個日期范圍列表(范圍-開始和范圍-結束)。G.10/06/1983 to 14/06/198315/07/1983 to 16/07/198318/07/1983 to 18/07/1983我想檢查另一個日期范圍是否包含列表中的任何一個范圍,我將如何做到這一點?G.06/06/1983 to 18/06/1983 = IN LIST10/06/1983 to 11/06/1983 = IN LIST14/07/1983 to 14/07/1983 = NOT IN LIST
3 回答

神不在的星期二
TA貢獻1963條經驗 獲得超6個贊
|-------------------| compare to this one |---------| contained within |----------| contained within, equal start |-----------| contained within, equal end |-------------------| contained within, equal start+end |------------| not fully contained, overlaps start |---------------| not fully contained, overlaps end |-------------------------| overlaps start, bigger |-----------------------| overlaps end, bigger |------------------------------| overlaps entire period
|-------------------| compare to this one |---| ends before |---| starts after
starts after end ends before start
|-------------| |-------| equal end with start of comparison period |-----| equal start with end of comparison period
SELECT *FROM periodsWHERE NOT (range_start > @check_period_end OR range_end < @check_period_start)
SELECT *FROM periodsWHERE range_start <= @check_period_end AND range_end >= @check_period_start

絕地無雙
TA貢獻1946條經驗 獲得超4個贊
where ('1983-06-06' <= end) and ('1983-06-18' >= start)

MM們
TA貢獻1886條經驗 獲得超2個贊
DELIMITER ;;CREATE FUNCTION overlap_interval(x INT,y INT,a INT,b INT)RETURNS INTEGER DETERMINISTICBEGINDECLARE overlap_amount INTEGER; IF (((x <= a) AND (a < y)) OR ((x < b) AND (b <= y)) OR (a < x AND y < b)) THEN IF (x < a) THEN IF (y < b) THEN SET overlap_amount = y - a; ELSE SET overlap_amount = b - a; END IF; ELSE IF (y < b) THEN SET overlap_amount = y - x; ELSE SET overlap_amount = b - x; END IF; END IF; ELSE SET overlap_amount = 0; END IF; RETURN overlap_amount;END ;;DELIMITER ;
添加回答
舉報
0/150
提交
取消