2012-01-18 107 views
6

我想建立一個酒店預訂系統。對於這個系統;數據庫也被用於來回的其他程序......但我有問題:在預訂前,我想看看哪個房間的號碼類型可供我爲我保留..酒店預訂系統Sql查詢?

我的表創建SQL querys

CREATE TABLE oteldb.oda (
oda_id INT (11) NOT NULL auto_increment, 
oda_tip_id INT (11) DEFAULT NULL, 
oda_adi VARCHAR (20) DEFAULT NULL, 
PRIMARY KEY (oda_id) 
) 
ENGINE = MyISAM 
AUTO_INCREMENT = 1 
CHARACTER SET utf8 
COLLATE utf8_general_ci; 

CREATE TABLE oteldb.tip (
tip_id INT (11) NOT NULL auto_increment, 
tip_adi VARCHAR (20) DEFAULT NULL, 
PRIMARY KEY (tip_id) 
) 
ENGINE = MyISAM 
AUTO_INCREMENT = 1 
CHARACTER SET utf8 
COLLATE utf8_general_ci 
ROW_FORMAT = FIXED; 

CREATE TABLE oteldb.rezervasyon (
rezervasyon_id INT (11) NOT NULL auto_increment, 
rezervasyon_gt DATE DEFAULT NULL, 
rezervasyon_ct DATE DEFAULT NULL, 
rezervasyon_oda_id INT (11) DEFAULT NULL, 
PRIMARY KEY (rezervasyon_id) 
) 
ENGINE = MyISAM 
AUTO_INCREMENT = 1 
CHARACTER SET utf8 
COLLATE utf8_general_ci; 

我試試這個,但現在

SELECT 
* 
FROM 
oteldb.tip 
WHERE 
IN tip.tip_id 
(SELECT 
oteldb.oda.oda_tip_id 
FROM 
oteldb.oda 
WHERE 
IN oda.oda_id note 

(SELECT 
oteldb.rezervasyon.rezervasyon_oda_id 
FROM 
oteldb.rezervasyon 
WHERE 
"2012-01-03" BETWEEN AND rezervasyon_ct rezervasyon_gt 
AND "2012-01-22" AND BETWEEN rezervasyon_gt rezervasyon_ct)) 

感謝不行......

+0

'凡在tip.tip_id'應該是'WHERE tip.tip_id IN ' – 2012-01-18 16:16:54

+2

+1包括DDL。 – 2012-01-18 16:27:02

+0

thanx弗洛林Ghita&馬克班尼斯特,我試過但結果是不正確的,也不能顯示房間的數量。我tihnk我的查詢是錯誤的... – Mehmet 2012-01-18 16:29:58

回答

2

假設可用客房是那些在任何時候在查詢期間已保留,並且rezervasyon_gt和rezervasyon_ct是分別預約開始日期和結束日期,請嘗試:

select t.tip_adi, count(oda.oda_tip_id) 
from oteldb.tip t 
left join (select oda_tip_id 
      from oteldb.oda o 
      where not exists 
       (select null 
       from oteldb.rezervasyon r 
       where r.rezervasyon_oda_id = o.oda_id and 
         r.rezervasyon_gt <= '2012-01-22' and 
         '2012-01-03' <= r.rezervasyon_ct) 
      ) oda on oda.oda_tip_id = t.tip_id 
group by t.tip_adi 
+0

感謝馬克查詢出現故障。 我的數據庫結構和數據sql查詢如下。謝謝您提前.. [Sql創建查詢](http://verigrup.net/oteldb.txt) 我轉換英文 – Mehmet 2012-01-18 20:24:38

+1

@Mehmet:使用您的測試數據,我試着在本地運行我的查詢,並意識到我應該選擇'count(oda.oda_tip_id)',而不是'count(*)'。我已經相應地修改了我的查詢 - 修改後的查詢似乎與測試數據正常工作。 – 2012-01-19 11:23:40

+0

感謝馬克,我再次嘗試查詢,但它也顯示預訂時間已被詢問的預約房間。 我向你發送英文[sql table](http://verigrup.net/hotel.txt)中的sql圖表鏈接。例如 例如2012,01,03和2012.01.22之間有5個空間。它不應該在「類型」列表中的「類型」中顯示佔用的房間。也就是說,如果8個房間中的5個房間在「type_id」= 1中被佔用,則應該記錄3個房間的「type_id」= 1。 – Mehmet 2012-01-19 18:37:35

0
select 
     RoomType.tip_adi, 
     sum(if(Rooms.oda_id = BookedRooms.rezervasyon_oda_id, 0, 1) as AvailableCount 
    from 
     oteldb.oda Rooms 

     LEFT JOIN (select distinct 
          res.rezervasyon_oda_id 
         from 
          oteldb.rezervasyo res 
         where 
           res.rezervasyon_gt between '2012-01-22' and '2012-01-03' 
          OR res.rezervasyon_ct between '2012-01-22' and '2012-01-03' 
        ) BookedRooms 
      on Rooms.oda_id = BookedRooms.rezervasyon_oda_id 

     JOIN oteldb.tip RoomType 
      on Rooms.oda_tip_id = RoomType.tip_id 
+0

DRapp謝謝,在查詢中輸入錯誤。 :/ 1您的SQL語法有錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以便在第3行Query1處使用'((Rooms.oda_id = BookedRooms.rezervasyon_oda_id,0,1))'作爲AvailableCount''附近的正確語法'QueryMeq 3 7 – Mehmet 2012-01-19 18:45:19

+0

@Mehmet,從IF(條件,真實答案,錯誤答案)更改爲Case/when條件。 – DRapp 2012-01-19 19:24:31

+0

:/ 1您的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在sum處使用正確的語法(在第1行查詢Rooms.oda_id = BookedRooms.rezervasyon_oda_id th時的情況)Query1.qry 1 37 – Mehmet 2012-01-20 22:16:27