酒店预订SQL查询不起作用

时间:2016-07-08 07:00:38

标签: php mysql sql select

我有两张桌子:

id  
category_id  
name  
description 
image  
price  
arrival_date  
depature_date  
status  

预订

id  
name  
email  
mobile  
category_id  
room_id  
arrival  
departure  

Q1。如何检查客房是否有空房?
Q2。我想显示用户输入日期为空的房间列表

我正在使用此查询,它给了我error #1241 - Operand should contain 1 column(s)

SELECT * FROM room WHERE status = '1' AND id NOT IN 
    (SELECT * FROM booking
    WHERE arrival='$arrival' 
    OR ( arrival BETWEEN '$arrival' AND '$departure' ) )

SELECT * FROM room WHERE status = '1' AND id NOT IN
    (SELECT * FROM booking WHERE arrival='2/08/2013'
    OR ( arrival BETWEEN '2/08/2013' AND '22/08/2013' ))

2 个答案:

答案 0 :(得分:3)

主要问题是,在内部查询中,您获得的列数太多了。

尝试:

SELECT * FROM room WHERE status = '1' AND id NOT IN
    ( SELECT room_id FROM booking
    WHERE arrival='$arrival'
    OR ( arrival BETWEEN '$arrival' AND '$departure' ))

SELECT * FROM room WHERE status = '1' AND id NOT IN
    ( SELECT room_id FROM booking
    WHERE arrival='2/08/2013'
    OR ( arrival BETWEEN '2/08/2013' AND '22/08/2013' ))

答案 1 :(得分:1)

为了您的问题:

  

Q1。我如何检查房间是否可用于客户

我想客户可以预订没有预订的房间,所以试试这个:

SELECT *
FROM room r
WHERE NOT EXISTS(
    SELECT 'booking'
    FROM booking b
    WHERE b.room_id = r.id
    AND  (b.arrival BETWEEN '$arrival' AND '$departure')
)
  

Q2。我想显示用户输入日期为空的房间列表

查询是相同的,如果在该时间间隔内没有任何预订房间是空的(因此可用于预订)