我具有以下数据库架构(其中日期值为0=mon,1=tue,2=wed
,依此类推)
以及以下输入
{
"day": 3,
"start_time": "15:00",
"end_time": "17:30"
},
{
"day": 5
"start_time": "08:00",
"end_time": "12:00"
}
我想查找输入值是否包含数据库中的任何重叠记录。 (请注意,就我而言,给定日期我可以有多个开始时间,结束时间)
这是我尝试过的。
SELECT
COUNT(id) AS total_count
FROM
working_hour
WHERE (
(day = 3 AND (start_time <= '15:00' AND end_time >= '17:30')) ||
(day = 5 AND (start_time <= '08:00' AND end_time >= '12:00'))
) AND user_id = 1;
这将返回总数0
,而我期望的是1
,因为在day = 3
上,我们有一个匹配的重叠记录,其ID为5
我在哪里错了?
谢谢。
答案 0 :(得分:1)
我相信您想要的逻辑是:
SELECT COUNT(*) AS total_count
FROM working_hour
WHERE user_id = 1 AND
((day = 3 AND start_time <= '17:30' AND end_time >= '15:00') OR
(day = 5 AND start_time <= '12:00' AND end_time >= '8:00')
) ;