MySQL查询与PHP之间的运算符之间的问题

时间:2019-02-25 06:28:08

标签: php mysql between difference-between-rows

我有MySQL表“ room_booking”,该表包含两列“ start_time”和“ end_time”。我想检查一下会议室的预定时间。假设从2:10 PM到3:15 PM预订了房间。然后,此时不应插入另一个条目。 我的查询是

$queryCheck = "SELECT start_time,end_time FROM room_booking AND (SELECT * FROM (room_booking) WHERE (start_time NOT BETWEEN '".$new_start_time."' AND '".$end_start_time."') AND (end_time NOT BETWEEN '".$new_start_time."' AND '".$end_start_time."'))";

2 个答案:

答案 0 :(得分:0)

在我看来,这只是重叠范围问题,在这种情况下,您想返回与输入开始时间和结束时间重叠的记录。尝试以下查询:

SELECT *
FROM room_booking
WHERE existing_end < ? OR existing_start > ?;

对于上面的两个?占位符,您将按顺序绑定$new_start_time$end_start_time。因此,您将得到以下查询:

SELECT *
FROM room_booking
WHERE existing_end > $new_start_time OR existing_start > $end_start_time;

答案 1 :(得分:0)

您可以使用这个:-

INSERT INTO `room_booking` (`room`,`start_time`,`end_time`) 
SELECT 'A','2019-02-25 07:55:00','2019-02-25 08:00:00' FROM `room_booking`
WHERE NOT EXISTS
    (SELECT * FROM `room_booking` WHERE '2019-02-25 07:55:00' BETWEEN `start_time` AND `end_time`)

让我知道您是否有疑问?

我附上屏幕截图以作解释enter image description here

enter image description here