I've been trying to solve this problem for ages and still have no luck.
I am trying to set up a vehicle database in where I check the availability of vehicles on dates in which I enter for people can rent them out. For example, they want to rent a vehicle from November 05/11/2016 till 20/11/2016.
I have been able to connect the query back to the vehicle table but when i test the query it either gives me all the vehicles being listed as available or none of them.
I have included the tables and the relationships as I think the table between each one may be causing me the problem.
Any suggestion to fix this code is much appreciated.
SELECT *
FROM Vehicles
WHERE Vehicles.vehicle_id NOT IN
(
SELECT distinct Booking.[vehicle id]
FROM Booking
WHERE (
[Enter Start Date] BETWEEN booking.start_rent_date
AND booking.end_rent_date
)
OR (
[Enter End Date] BETWEEN booking.start_rent_date
AND booking.end_rent_date
)
);
I think of this as a
left join
:A vehicle is not free if there is a rental that starts before the desired end date and that ends after the desired start date. The
left join
excludes these. You could also useNOT EXISTS
orNOT IN
for equivalent logic.EDIT:
Oh, you are using MS Access . . .