0

我有一个带有 2 张桌子的酒店系统

预订表

reservation_id pk
Start_Date
End_Date
Customers_Customer_id fk
Rooms_Room_id fk

客房表

Room_id pk
type
floor
price

我有这个query来检索可用的房间

$sql="SELECT Room_id,type,room_floor,price FROM rooms
LEFT JOIN reservation
ON rooms.Room_id = reservation.Rooms_Room_id
where rooms.type='$_POST[type]'
and Start_Date !='$_POST[from]'
and End_Date !='$_POST[to]'"

所以结果很好,但它只是给了我预订表中的房间

4

1 回答 1

0

当您在 where 子句中的左外连接表上添加条件时,它的行为类似于内连接,这应该修复您当前的查询,

$sql="SELECT Room_id,type,room_floor,price FROM rooms
LEFT JOIN reservation
ON rooms.Room_id = reservation.Rooms_Room_id
and Start_Date !='$_POST[from]'
and End_Date !='$_POST[to]'
where rooms.type='$_POST[type]'"

但是您不会返回正确的结果,您可能希望将查询更改为此

$sql="SELECT Room_id,type,room_floor,price FROM rooms 
WHERE room_id not in 
(
   --find all rooms that are unavailable
   SELECT Room_id FROM rooms
   INNER JOIN reservation ON rooms.Room_id = reservation.Rooms_Room_id
   WHERE NOT ('$_POST[to]' < start_date or '$_POST[from]' > end_date)
   AND rooms.type='$_POST[type]'
) AND type='$_POST[type]'"
于 2013-04-23T05:04:11.967 回答