我想检查预订表中的所有详细信息,如果他们在访客入住和退房日期范围内。客人选择的入住日期为'new_date_in'并且签出日期是'new_date_out'。
Runnable
Bur问题是这个早期检索不在范围内。
实施例。如果客人选择'new_date_in'如2016/09/25和'new_date_out'作为房间ID 3的2016/09/28
返回此输出
$SQL="SELECT * FROM bookings WHERE roomid = '$roomid' AND (( bookings.checkindate < '$new_date_in' AND bookings.checkoutdate >= '$new_date_in')
OR (bookings.checkindate < '$new_date_out' AND bookings.checkoutdate > '$new_date_out' )
OR (bookings.checkindate >= '$new_date_in' AND bookings.checkoutdate <= '$new_date_out' )) ;
不应将其作为输出返回。请检查我的SQL查询并更正它。
谢谢。
答案 0 :(得分:0)
对我来说很好。我得到零结果:
CREATE TABLE bookings (
bookingid int primary key not null,
checkindate date not null,
checkoutdate date not null,
roomid int not null,
guestid int not null,
qty int not null
);
insert into bookings (bookingid, checkindate, checkoutdate, roomid, guestid, qty)
values
(524,'2016-08-01','2016-08-03',2,85,1),
(525,'2016-10-12','2016-10-15',3,86,1),
(526,'2016-07-23','2016-07-31',2,87,3),
(527,'2016-07-26','2016-07-28',1,88,1),
(533,'2016-07-28','2016-08-02',2,96,1),
(534,'2016-09-08','2016-09-10',3,97,1),
(535,'2016-08-03','2016-08-05',3,98,2);
SELECT *
FROM bookings
WHERE roomid = 3
AND (
(bookings.checkindate < '2016-09-25' AND bookings.checkoutdate >= '2016-09-25')
OR (bookings.checkindate < '2016-09-28' AND bookings.checkoutdate > '2016-09-28')
OR (bookings.checkindate >= '2016-09-25' AND bookings.checkoutdate <= '2016-09-28')
)
似乎不是SQL逻辑错误。这留下了以下可能出现的问题:
$SQL
执行之前,请先查看实际值。