我有以下3个MySQL表。
我将预订存储在bookings表中&事件日期和时间存储在单独的MySQL表中。
我希望有一个MySQL查询来列出特定日期的可用时间。
因此,如果输入值日期1,它将不会显示可用的时间,但如果输入2,它将输出1 | 9:00。
INSERT INTO `bookings` (`id`, `email`, `date_requested`, `time_requested`) VALUES
(1, 'test@test.com', '1', '1'),
(2, 'test2@test.com', '1', '2'),
(3, 'test3@test.com', '2', '2');
INSERT INTO `bookings_dates` (`id`, `date`) VALUES
(1, '2022-11-05'),
(2, '2022-11-06'),
(3, '2022-11-07');
INSERT INTO `bookings_times` (`id`, `time`) VALUES
(1, '9:00'),
(2, '9:15');发布于 2022-11-03 20:27:32
您可以使用NOT IN条件。
SELECT *
FROM bookings_times
WHERE id NOT IN (
SELECT time_requested
FROM bookings
WHERE date_requested = (
SELECT id
FROM bookings_dates
WHERE date = '2022-11-05'
)
)演示
发布于 2022-11-03 20:39:50
为了获取给定一天(1)的时间,您可以使用以下查询:
SELECT
t.`id`, t.`time`
FROM
bookings_times AS t
INNER JOIN bookings AS b ON b.time_requested = t.id
WHERE
b.date_requested = 1Edit1:
为了获得某一天内未使用的时间(1),您可以使用以下查询:
SELECT
t.`id`, t.`time`
FROM
bookings_times AS t
LEFT JOIN bookings AS b ON b.time_requested = t.id AND (b.date_requested = 1)
WHERE
IsNull(b.time_requested)https://stackoverflow.com/questions/74308950
复制相似问题