我的sql数据库中有3个表:
Table Room
roomnumber roomtype
1 DeluxeRoom
2 DeluxeRoom
3 DeluxeRoom
100 StandardRoom
101 StandardRoom
102 StandardRoom
房间是存放房间及其类型的桌子。
Table Reservation
reservationid checkin checkout
1 2017-01-01 2017-01-03
2 2017-01-10 2017-01-20
3 2017-02-05 2017-02-08
预订是用户预订的表格。
Table RoomReserve
reservationid roomnumber
1 1
2 100
2 101
3 1
3 2
3 3
在RoomReserve中,对于每个reservationid,我可以为roomnumber输入多个值。如果签入和结账日期不同,我也可以为不同的预订单输入相同的房间号。
我正在尝试在php中进行查询,以便在用户想要预订房间时计算可用的房间,但我得到的结果是错误的。
以下是我正在尝试的代码:
1) $sql="SELECT COUNT(roomnumber) FROM room WHERE roomtype='DeluxeRoom' AND roomnumber NOT IN (
SELECT * FROM roomreserve r,reservation e, room m WHERE r.reservation_id=e.reservation_id AND m.roomnumber=r.roomnumber
AND ('$checkin' BETWEEN start_date AND end_date) OR ('$checkout' BETWEEN start_date AND end_date))";
2) $sql="SELECT COUNT(roomnumber) FROM room WHERE roomtype='DeluxeRoom' AND roomnumber NOT IN (
SELECT COUNT(roomnumber) FROM roomreserve r,reservation e, room m WHERE r.reservation_id=e.reservation_id AND m.roomnumber=r.roomnumber
AND ('$checkin' BETWEEN start_date AND end_date) OR ('$checkout' BETWEEN start_date AND end_date))";
答案 0 :(得分:-1)
如果客户目前已入住房间,您在结帐栏中有什么?如果它为空,那么您可以使用checkout is null
进行查询,或者根据您的需要修改该部分。
select count(*) from room where roomtype = 'DeluxeRoom' and roomnumber not in (
select room.roomnumber from room, reservation, roomreserve where room.roomtype = 'DeluxeRoom' and roomreserve.roomnumber = room.roomnumber and roomreserve.reservationid = reservation.reservationid and checkout is null)
您还可以在Reservation表中添加checked_out标志,以便于查询。
答案 1 :(得分:-1)
在任何时候你都没有说你的内容有什么问题,但假设这个问题是语法上的(即你根本无法得到执行的语句),那就试试吧:
SET @start_date = '2017-01-01';
SET @end_date = '2017-01-10';
SELECT COUNT(roomnumber) FROM room WHERE roomtype='DeluxeRoom' AND roomnumber NOT IN (
SELECT r.roomnumber FROM roomreserve r,reservation e, room m WHERE r.reservationid=e.reservationid AND m.roomnumber=r.roomnumber
AND ((checkin BETWEEN @start_date AND @end_date) OR (checkout BETWEEN @start_date AND @end_date)));
至少那是一个有效的SQL语句。根据需要用PHP包装。
c& p easy的完整测试用例:
DROP TABLE Room;
DROP TABLE Reservation;
DROP TABLE RoomReserve;
CREATE TABLE Room (
roomnumber int,
roomtype varchar(255)
);
INSERT INTO Room
VALUES
(1, 'DeluxeRoom'),
(2, 'DeluxeRoom'),
(3, 'DeluxeRoom'),
(100, 'StandardRoom'),
(101, 'StandardRoom'),
(102, 'StandardRoom');
CREATE TABLE Reservation (
reservationid int,
checkin date,
checkout date
);
INSERT INTO Reservation
VALUES
(1, '2017-01-01', '2017-01-03'),
(2, '2017-01-10', '2017-01-20'),
(3, '2017-02-05', '2017-02-08');
CREATE TABLE RoomReserve (
reservationid int,
roomnumber int
);
INSERT INTO RoomReserve
VALUES
(1, 1),
(2, 100),
(2, 101),
(3, 1),
(3, 2),
(3, 3);
SET @start_date = '2017-01-01';
SET @end_date = '2017-01-10';
SELECT COUNT(roomnumber) FROM room WHERE roomtype='DeluxeRoom' AND roomnumber NOT IN (
SELECT r.roomnumber FROM roomreserve r,reservation e, room m WHERE r.reservationid=e.reservationid AND m.roomnumber=r.roomnumber
AND ((checkin BETWEEN @start_date AND @end_date) OR (checkout BETWEEN @start_date AND @end_date)));
我得到" 2"作为回应,这是在2017-01-01和2017-01-10(第2和第3房间)之间没有预订日期的DeluxeRoom的数量。