2

我正在用 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)

但我只想要未预订房间的数据。如果房间已经被预订,那么它必须表明这个房间在这个日期已经被预订了。

我对此一无所知。

4

4 回答 4

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); 
于 2012-12-27T11:59:37.967 回答
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')
    )
于 2012-12-27T12:06:10.820 回答
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
于 2012-12-27T12:13:43.837 回答
0

试试这样,你会得到d个没有预订的房间

SELECT * FROM room r WHERE 
 not exists (
   SELECT cb.room_no FROM customer_booking cb where cb.room_no=r.room_no);
于 2012-12-27T12:35:26.763 回答