Question

There are two tables, hotel and hotel_booking. The hotel_booking table has two columns:

hotel_id
date_booked 

The hotel_id goes from 1-10, and for each one, a date will be entered if it has been booked.

I need to search for all the hotel_id's that have not been booked for a particular date. I have tried using NOT IN, but it returns all the dates that do not match the one entered, so does not equal to.

This doesn't work, but this is what I've got so far:

SELECT * FROM hotel_booking WHERE date != "29/01/2014"

I know I have to do a sub query of some sort, but I don't know where to start.

Was it helpful?

Solution

You could use this query:

SELECT h.hotel_id
FROM hotel h
  LEFT JOIN (SELECT * 
              FROM hotel_booking b
              WHERE date = "29/01/2014"
             ) excluded
  ON excluded.hotel_id = h.hotel_id
WHERE excluded.hotel_id IS NULL

Or also this one:

SELECT hotel_id
FROM hotel h
WHERE NOT EXISTS (SELECT NULL
                  FROM hotel_booking hb
                  WHERE hb.hotel_id = h.hotel_id
                  AND date = "29/01/2014")

OTHER TIPS

SELECT * FROM hotel_booking WHERE date != "2014-01-29"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top