boney alex Posted April 4, 2007 Share Posted April 4, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/45638-solved-problem-with-select-query/ Share on other sites More sharing options...
MadTechie Posted April 4, 2007 Share Posted April 4, 2007 i Guess something like:| SELECT * WHERE `Table Vehicle`.`MakeModelID` = '3' AND `Table Vehicle`.`BranchID` = '1' AND `Table Reservation`.`` = '2007-04-12' *untested Quote Link to comment https://forums.phpfreaks.com/topic/45638-solved-problem-with-select-query/#findComment-221673 Share on other sites More sharing options...
Wildbug Posted April 4, 2007 Share Posted April 4, 2007 SELECT syntax MySQL tutorial SELECT * FROM vehicle JOIN registration ON vehicle.vehicleID=registration.vehicleID WHERE makemodelID=3 AND branchID=1 AND NOT (collect_date <= 20070409 AND return_date > 20070409); Quote Link to comment https://forums.phpfreaks.com/topic/45638-solved-problem-with-select-query/#findComment-221679 Share on other sites More sharing options...
boney alex Posted April 5, 2007 Author Share Posted April 5, 2007 hi wildbug, i tried that code but just like my attempts, it failed to recognise the other vehicle DY53 WAJ, because that isnt in the reservation table. Quote Link to comment https://forums.phpfreaks.com/topic/45638-solved-problem-with-select-query/#findComment-221686 Share on other sites More sharing options...
btherl Posted April 5, 2007 Share Posted April 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/45638-solved-problem-with-select-query/#findComment-221731 Share on other sites More sharing options...
boney alex Posted April 5, 2007 Author Share Posted April 5, 2007 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/45638-solved-problem-with-select-query/#findComment-221741 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.