我写了一个旅馆房间的预订系统。 我在数据库中有一张表,其中包含酒店的所有房间(房间)。下表是预订。我需要编写一个函数,该函数将为我提供预订的日期,开始和结束时间,并且该函数将在给定的期限内将所有空闲房间退还给我。我不知道该如何完全咬它。有人可以指导我吗?
我当时正在考虑获取所有房间,然后进行所有预订,并在有效日期之前发货,但是接下来呢?怎么安排呢?
@edit
房间表:
- id
- number
预订表:
- id
- room_id
- date
- time_start
- time_finish
答案 0 :(得分:2)
您可以执行以下操作:
select rooms.* from rooms
left join reservations
on rooms.id = reservations.room_id
and reservations.date = '2019-05-28' and reservations.time_start < '14:00' and reservations.time_finish > '14:00'
where reservations.id is null
如果测试模式如下:
SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
CREATE TABLE `reservations` (
`id` int(11) NOT NULL,
`room_id` int(11) NOT NULL,
`date` date NOT NULL,
`time_start` time NOT NULL,
`time_finish` time NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `reservations` (`id`, `room_id`, `date`, `time_start`, `time_finish`) VALUES
(0, 2, '2019-05-28', '13:00:00', '16:00:00');
CREATE TABLE `rooms` (
`id` int(11) NOT NULL,
`name` varchar(15) NOT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `rooms` (`id`, `name`) VALUES
(1, 'room1'),
(2, 'room2');
小提琴示例: