我试图让一个酒店预订系统返回的房间id的(我使用的房间号码)的房间,没有预订,是一个特定的房间大小,在这种情况下,单人。
这是我目前正在使用的
SELECT *
FROM rooms
WHERE room_id NOT IN(SELECT room_id
FROM room_bookings
WHERE '2021/04/27' BETWEEN check_in_date AND check_out_date OR '2021/04/30' BETWEEN check_in_date AND check_out_date)
AND NOT EXISTS (SELECT * FROM rooms WHERE room_size = 'Double')
返回房间id的,目前没有预订的作品,但我想它只返回房间id的是单一的,不包括双人房。
我需要向该查询添加什么才能使其排除双人间?
CREATE TABLE `rooms` (
'room_id` int(11) NOT NULL,
`room_size` varchar(255) NOT NULL,
`floor_number` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `rooms` (`room_id`, `room_size`, `floor_number`) VALUES
(1, 'UNASSIGNED', 'UNASSIGNED'),
(2, 'Double', 'First'),
(3, 'Single', 'First'),
(4, 'Double', 'First'),
(5, 'Single', 'First'),
(6, 'Double', 'Second'),
(7, 'Single', 'Second'),
(8, 'Double', 'Second'),
(9, 'Single', 'Second'),
(10, 'Double', 'Second');
CREATE TABLE `room_bookings` (
`booking_id` int(11) NOT NULL,
`customer_id` int(11) NOT NULL,
`room_id` int(11) NOT NULL,
`manager_id` int(11) NOT NULL,
`check_in_date` date NOT NULL,
`check_out_date` date NOT NULL,
`booking_date` date NOT NULL,
`room_size` varchar(255) NOT NULL,
`lunch` varchar(255) NOT NULL,
`dinner` varchar(255) NOT NULL,
`price` varchar(255) NOT NULL,
`booking_status` varchar(255) NOT NULL,
`payment_status` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `room_bookings` (`booking_id`, `customer_id`, `room_id`, `manager_id`, `check_in_date`, `check_out_date`, `booking_date`, `room_size`, `lunch`, `dinner`, `price`, `booking_status`, `payment_status`) VALUES
(9, 6, 1, 1, '2021-04-27', '2021-04-30', '2021-04-27', 'Double', '2', '2', '£230', 'Pending', 'test'),
(10, 6, 3, 1, '2021-04-29', '2021-05-04', '2021-04-27', 'Double', '3', '4', '£465', 'Pending', 'test'),
(11, 6, 7, 1, '2021-04-28', '2021-04-30', '2021-04-27', 'Double', '0', '0', '£120', 'Pending', 'test'),
(12, 6, 1, 1, '2021-04-29', '2021-05-04', '2021-04-27', 'Double', '2', '2', '£350', 'Pending', 'No Payment');
5条答案
按热度按时间cyvaqqii1#
问题在于这种说法:
SELECT *
FROM rooms r
WHERE room_id NOT IN(SELECT room_id
FROM room_bookings
WHERE '2021/04/27' BETWEEN check_in_date AND check_out_date
OR '2021/04/30' BETWEEN check_in_date AND check_out_date)
AND NOT EXISTS (SELECT room_id FROM rooms d
WHERE d.room_size = 'Double'
AND d.room_id = r.room_id)
SELECT *
FROM rooms r
WHERE room_id NOT IN (SELECT room_id
FROM room_bookings
WHERE '2021/04/27' BETWEEN check_in_date AND check_out_date
OR '2021/04/30' BETWEEN check_in_date AND check_out_date)
AND room_id NOT IN (SELECT room_id
FROM rooms
WHERE room_size = 'Double')
SELECT *
FROM rooms r
WHERE room_id NOT IN (SELECT room_id
FROM room_bookings
WHERE '2021/04/27' BETWEEN check_in_date AND check_out_date
OR '2021/04/30' BETWEEN check_in_date AND check_out_date)
AND room_size <> 'Double'
lg40wkob2#
在创建table房间时出现了一个小错误
你可以把所有的人都联合起来
km0tfn4u3#
你的
EXISTS
条款不合适。你只想要单人房,所以用吧WHERE room_size = 'Single'
:rmbxnbpk4#
我也想出了这个解决办法
dsf9zpds5#
checkout 以下查询: