2013-04-23 224 views
0

我有一個酒店系統thesw 2表MySQL查詢(加入)

預約表

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]'" 

所以結果是好的,但它只是給我的預約表 房間我想在預約表,但不是在搜索日期 房間加上在房間表中的其他房間

+0

沒有太多的一個mysql的傢伙,但是不會使用'$ _POST'!首先消毒!閱讀這裏http://stackoverflow.com/questions/3645131/what-is-a-good-method-to-sanitize-the-whole-post-array-in-php AND here http://stackoverflow.com/questions/4861053/php-sanitize-values-of-a-array/ – Grmn 2013-04-23 05:06:14

+0

認爲你知道該網站是安全湖它只是一個項目,我的拼貼它不會在網上公佈 – user2309797 2013-04-23 05:26:50

回答

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]'" 
+0

感謝人改變了地方後像一個魅力的房間類型 – user2309797 2013-04-23 05:24:46