MySQL 检查给定的时隙是否在给定的时隙之间
MySQL check if given timeslot is in between provided timeslot
我有预约 table 类似:
| id | user_id | guest_name | appointment_date | start_time | end_time |
|----|---------|------------|------------------|------------|----------|
| 1 | 1 | Abc | 2016-09-30 | 13:00:00 | 13:30:00 |
考虑到名为 "Abc" 的客人在日期 2016-09-30
13:00
到 13:30 pm
有预约,我想检查提供的时间段(即13:00-13:30
) 日期 2016-09-30
不得与后续日期时间输入重叠。
| appointment_date | start_time | end_time |
|------------------|------------|----------|
| 2016-09-30 | 11:00 | 12:00 |
| 2016-09-30 | 12:30 | 13:00 |
| 2016-09-30 | 13:30 | 14:00 |
| 2016-09-30 | 14:00 | 14:30 |
| 2016-09-30 | 12:45 | 13:15 |
| 2016-09-30 | 13:15 | 13:45 |
| 2016-09-30 | 13:10 | 13:20 |
| 2016-09-30 | 13:00 | 13:25 |
| 2016-09-30 | 13:10 | 13:30 |
| 2016-09-30 | 13:00 | 13:30 |
| 2016-10-01 | 13:00 | 13:30 |
考虑到以下输入,您可能会看到以 13:00
到 13:30
开始或结束的时段与预约 table 中的记录重叠。
简而言之,如果提供的输入的开始和结束时间与它们重叠,我想要一个查询 returns 约会预订的记录 table。
我试过以下方法:
SELECT * FROM appointments
WHERE user_id = 1
AND appointment_date = DATE('2016-09-30')
AND ( (start_time = '11:00' AND end_time = '12:00')
OR
(start_time < '11:00' AND end_time > '12:00')
OR
(start_time > '11:00' AND end_time < '12:00')
OR (
(start_time > '11:00' OR end_time < '11:00')
AND
(start_time < '12:00' OR end_time < '12:00'))
);
但是没有成功。有谁知道如何检查它?
您可以使用以下查询:
SELECT DISTINCT t1.*
FROM appointments t1
JOIN dates t2 ON t1.appointment_date = t2.appointment_date AND
t1.start_time <= t2.end_time AND
t1.end_time >= t2.start_time
以上查询returnstable的记录appointments
只要有至少一条条重叠的记录在其他table。
我有预约 table 类似:
| id | user_id | guest_name | appointment_date | start_time | end_time |
|----|---------|------------|------------------|------------|----------|
| 1 | 1 | Abc | 2016-09-30 | 13:00:00 | 13:30:00 |
考虑到名为 "Abc" 的客人在日期 2016-09-30
13:00
到 13:30 pm
有预约,我想检查提供的时间段(即13:00-13:30
) 日期 2016-09-30
不得与后续日期时间输入重叠。
| appointment_date | start_time | end_time |
|------------------|------------|----------|
| 2016-09-30 | 11:00 | 12:00 |
| 2016-09-30 | 12:30 | 13:00 |
| 2016-09-30 | 13:30 | 14:00 |
| 2016-09-30 | 14:00 | 14:30 |
| 2016-09-30 | 12:45 | 13:15 |
| 2016-09-30 | 13:15 | 13:45 |
| 2016-09-30 | 13:10 | 13:20 |
| 2016-09-30 | 13:00 | 13:25 |
| 2016-09-30 | 13:10 | 13:30 |
| 2016-09-30 | 13:00 | 13:30 |
| 2016-10-01 | 13:00 | 13:30 |
考虑到以下输入,您可能会看到以 13:00
到 13:30
开始或结束的时段与预约 table 中的记录重叠。
简而言之,如果提供的输入的开始和结束时间与它们重叠,我想要一个查询 returns 约会预订的记录 table。
我试过以下方法:
SELECT * FROM appointments
WHERE user_id = 1
AND appointment_date = DATE('2016-09-30')
AND ( (start_time = '11:00' AND end_time = '12:00')
OR
(start_time < '11:00' AND end_time > '12:00')
OR
(start_time > '11:00' AND end_time < '12:00')
OR (
(start_time > '11:00' OR end_time < '11:00')
AND
(start_time < '12:00' OR end_time < '12:00'))
);
但是没有成功。有谁知道如何检查它?
您可以使用以下查询:
SELECT DISTINCT t1.*
FROM appointments t1
JOIN dates t2 ON t1.appointment_date = t2.appointment_date AND
t1.start_time <= t2.end_time AND
t1.end_time >= t2.start_time
以上查询returnstable的记录appointments
只要有至少一条条重叠的记录在其他table。