Jump to content

[SOLVED] Problem with SELECT query


boney alex

Recommended Posts

Hi, Im currently building a vehicle rental website.

Im workin on a query which is trying to select potential  vehicles for reservation but only if it satisfies some certain characteristics:

  1) it must be held at the branch (ive been using BranchID = 1 for test purposes)

  2) it must be of a certain make and model (ive been using MakeModelID = 3 for test purposes)

  3) the query must EXCLUDE those vehicles currently reserved. For this I've been using NOT BETWEEN Collect_Date AND Return_Date.

 

Table Vehicle

 

VehicleID  Registration  MakeModelID  BranchID

    1          AE02 FWC          3              1

    2          DY53 WAJ            3              1

 

Table Reservation

 

ReserID  CustID  VehicleID  Collect_Date  Return_Date

    1        1            1          2007-04-04      2007-04-11     

 

I need a query that will show both AE02 FWC and DY53 WAJ if the user selects:

* 3 for the MakeModelID

* 1 for the BranchID

* 2007-04-12 for the collect date

 

And only show DY53 WAJ if the collect date was for example 2007-04-09.

 

Any help would be much appreciated. Thanks. Alex

Link to comment
Share on other sites

If the query is fine apart from not returning cars which are not in the reservations table, you can use a left join

 

SELECT * FROM vehicle
LEFT JOIN registration ON vehicle.vehicleID=registration.vehicleID
WHERE makemodelID=3
AND branchID=1
AND (
   NOT (collect_date <= 20070409 AND return_date > 20070409)
   OR (reserID IS NULL)
);

 

reserID will be null for those rows where there is no data from the reservations (registration?) table.

Link to comment
Share on other sites

thanks btherl

 

think ive sorted it using a very similar query:

SELECT *
FROM Vehicle
WHERE BranchID = '1'
AND MakeModelID = '3'
AND VehicleID NOT 
IN (
SELECT VehicleID
FROM Reservation
WHERE '2007-04-03'
BETWEEN Collection_Date
AND Return_Date)

 

Both seem to return identical results. Thanks for your help!!!

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.