简体   繁体   中英

hotel booking SQL query not working

I have a two tables:

Room

id  
category_id  
name  
description 
image  
price  
arrival_date  
depature_date  
status  

Booking

id  
name  
email  
mobile  
category_id  
room_id  
arrival  
departure  

Q1. How can I check if a room is available or not for customers?
Q2. I want to show the room list that is empty for user input date

I am using this query and it gives me 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' ))

The main problem is that, on the inner query, you are getting too many columns as results.

Try:

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' ))

In order to your questions:

Q1. how can i check room is available or not for customers

I suppose a room without booking is available for customers, so try this:

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. i want to show thous room list that is empty for user input date

The query is the same, if doesn't exist any booking in that interval the room is empty (so is available for booking)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM