我正在做php和mysql的酒店预订系统项目 我创建了下面的房间表
room_no| roo_id | room_type
D1 | 1 | Deluxe
R1 | 2 | AC
R2 | 3 | Non Ac
C1 | 4 | Cottage
此表是关于房间代表酒店的总房间。
这是customer_booking表,
customer_id | Cust_name | email | Address | room_type | room_no| cost|check_in | check_out
1 | satyawan |ss@gmail.com| xy | AC | R1 |5000 | 2012-12-26|2012-12-30
2 |satyawan | lahu@gmail.com|unr | Non A/C | R6 | 20002012-01-01|2012-12-07
3 |satyawan | lahu@gmail.com|unr | Non A/C | R8 | 2013-01-01 |2013-01-10
4 |satyawan | lahu@gmail.com|unr | Non A/C | R9 | 2013-01-10 |2013-01-16
5 |satyawan | lahu@gmail.com|unr | Non A/C | R6 | 2013-01-01 |2013-01-20
我已经针对未预订房间的排序数据触发了这两个表的连接查询。查询在
之下SELECT *
FROM rooms
LEFT JOIN customer_booking
ON customer_booking.room_no = rooms.room_no
WHERE customer_booking.client_id IS NULL
OR (customer_booking.check_out >= 2013-01-01
AND customer_booking.check_in >= 2013-01-10)
OR (customer_booking.check_out <= 2013-01-01
AND customer_booking.check_out <= 2013-01-10)
但我只想要未预订房间的数据。 如果房间已经预订,则必须显示此房间已在此日期预订。
我无法理解它。
答案 0 :(得分:1)
也许您正在寻找NOT IN
。
SELECT * FROM rooms AS r WHERE r.room_no NOT IN (
SELECT cb.room_no FROM customer_booking AS cb WHERE your_checkin_constraints);
答案 1 :(得分:0)
尝试使用此查询。如果最后一个字段room_customer_id
为0,那么房间是免费的,否则它显示customer_id谁保留了这个房间。
SELECT rooms.*,COALESCE(customer_booking.customer_id,0) room_customer_id
FROM rooms
LEFT JOIN customer_booking ON
(customer_booking.room_no = rooms.room_no)
and
(
('2013-01-01' between customer_booking.check_in and customer_booking.check_out)
or
('2013-01-10' between customer_booking.check_in and customer_booking.check_out)
or
(customer_booking.check_in between '2013-01-01' and '2013-01-10')
)
答案 2 :(得分:0)
SELECT r.room,case when r.room_no is null then 'Available'
else 'Not Available' end
FROM rooms r
LEFT JOIN customer_booking cb
ON cb.room_no = r.room_no
AND cb.check_out <= cur_date()-1
答案 3 :(得分:0)
尝试这样,然后你会得到没有预订的房间
SELECT * FROM room r WHERE
not exists (
SELECT cb.room_no FROM customer_booking cb where cb.room_no=r.room_no);