|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Combining 2 Queries
I want to combine two queries. The database is for a car rental agency. I want to show all cars that have never been rented and that are free on a the date of viewing the query. Here is the code of the two individual queries.
SELECT Vehicle.VehicleNo, Vehicle.Make FROM Vehicle LEFT JOIN Rental ON Vehicle.VehicleNo = Rental.VehicleNo WHERE (((Rental.VehicleNo) Is Null)); * this shows all vehicles which have never ever been rented SELECT Vehicle.VehicleNo, Rental.DateReturn FROM Vehicle INNER JOIN Rental ON Vehicle.VehicleNo = Rental.VehicleNo GROUP BY Vehicle.VehicleNo, Rental.DateReturn HAVING (((Rental.DateReturn)<Date())); this one show the vehicles which are free at todays date I want to combine these two queries in one. Any IDeas |
|
#2
|
|||
|
|||
|
Try a Union Statement like below
SELECT Vehicle.VehicleNo, Vehicle.Make, Rental.DateReturn, 'Never Used' as Used FROM Vehicle LEFT JOIN Rental ON Vehicle.VehicleNo = Rental.VehicleNo WHERE (((Rental.VehicleNo) Is Null)) UNION SELECT Vehicle.VehicleNo, Vehicle.Make, Rental.DateReturn, 'Returned' as Used FROM Vehicle INNER JOIN Rental ON Vehicle.VehicleNo = Rental.VehicleNo GROUP BY Vehicle.VehicleNo, Rental.DateReturn HAVING (((Rental.DateReturn)<Date())); S- |
|
#3
|
||||
|
||||
|
you could try something like this.
not sure what table 'Never Used' or 'Returned' are coming from. Code:
SELECT A.VehicleNo, A.Make, B.DateReturn FROM Vehicle As A INNER JOIN Rental As B On (A.VehicleID = B.VehicleID) WHERE A.VehicleNo Is Null AND B.DateReturn < Date() |
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > Combining 2 Queries |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|