Reputation: 149
I want to build a Hotel Reservation System. For this system; database is also used fro an other program... But i have problem: before the reservation i want to see which number of rooms type are available for my for my reservation..
My table create 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;
i try this but not work
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))
thanks now...
Upvotes: 6
Views: 3590
Reputation: 48139
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
Upvotes: 0
Reputation:
Assuming that available rooms are those that are not already reserved at any time during the query period, and that rezervasyon_gt and rezervasyon_ct are the reservation start and end dates respectively, try:
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
Upvotes: 2