在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
提交
取消