如何检查您要为 activity 计划的开始和结束时间是否尚未计划?

How to check if the start and end time that you want to plan for an activity aren't already planned?

我需要进行查询以检查用户想要计划的开始时间和结束时间是否尚未计划好

我目前有这个查询:

select *
from planned_activities
where 
    user_id = 161
and
    '2022-01-11 17:36:00' between start_time and end_time
or '2022-01-11 18:36:00' between start_time and end_time
or ('2022-01-11 17:36:00' <= start_time and '2022-01-13 18:36:00' >= end_time);

我觉得很难解释,但我基本上想 return 来自其他计划的数据 activity 如果计划不可能。

SELECT EXISTS ( SELECT NULL
                FROM planned_activities
                WHERE @current_user_id = planned_activities.user_id 
                  AND @planned_activity_start < planned_activities.end_time
                  AND @planned_activity_end > planned_activities.start_time ) row_exists

查询检查当前用户是否存在与输入的时间范围重叠的行。 Returns一行一列row_exists,可能的值为1(找到重叠)或0(输入的时间范围是自由的)。

如果相邻时间范围也不允许,则使用弱比较运算符。