简体   繁体   中英

SQL nested select statement based on dates

This is for a small car rental website.

I have a search facility which searches for all the cars in the CAR table, which takes 2 parameters: a From date and a To date - the dates the customer wants to rent the car for. There are also drop down boxes from which customers can specify which types of cars they want to see.

My current method of returning cars is a stored procedure:

CREATE PROCEDURE SearchForCars(
    @Make VARCHAR(50), 
    @Model VARCHAR(50), 
    @Age INT, 
    @Transmission VARCHAR(50), 
    @Colour VARCHAR(50),
    @FuelType VARCHAR(50)
AS
BEGIN 
    SELECT 
       Make, Model, Registration, Age, Transmission, Body_Type, Colour, 
       Estimated_Value, Mileage, Fuel_Type, Rental_Price_Per_Day 
    FROM 
       HAS_Car
    WHERE 
       Make LIKE '%' + @Make + '%'
       AND Model LIKE '%' + @Model + '%'
       AND Age = Age
       AND Transmission LIKE '%' + @Transmission + '%'
       AND Colour LIKE '%' + @Colour + '%'
       AND Fuel_Type LIKE '%' + @FuelType + '%'
END;

I also have a bookings table which holds the car_id , customer_id , to_date and from_date .

In the main search I want to only return the cars which have not been booked based on the dates the customer specifies.

This involves joining the car table to the bookings table but I do not know how to do this.

For example, this is part of the car table:

+--------+----------+-------+---------+---+-----------+-----------+-----------+-------+-------+--------+----+
|      8 | Vauxhall | Corsa | LO10RTY | 4 | Manual    | Hatchback | White     |  7000 | 48000 | Diesel | 20 |
|      9 | Audi     | TT    | KN13DOP | 1 | Manual    | Coupe     | White     | 35000 | 10000 | Petrol | 55 |
|     10 | Toyota   | Yaris | AS12UWS | 1 | Automatic | Hatchback | Navy Blue | 11000 |  5000 | Petrol | 25 |
+--------+----------+-------+---------+---+-----------+-----------+-----------+-------+-------+--------+----+

This is the bookings table:

Booking_ID   Customer_ID  Car_ID  From_Date     To_Date
       1           8         9    01/01/2015    03/01/2015

If I now search for a car number 9, renting it from 02/01/2015 to 13/01/2015 then car number 9 (audi tt) should not show up in the main search as it has an overlaping booking already.

Try this.

SELECT 
       Make, Model, Registration, Age, Transmission, Body_Type, Colour, 
       Estimated_Value, Mileage, Fuel_Type, Rental_Price_Per_Day 
    FROM 
       HAS_Car H
    WHERE 
       Make LIKE '%' + @Make + '%'
       AND Model LIKE '%' + @Model + '%'
       AND Age = Age
       AND Transmission LIKE '%' + @Transmission + '%'
       AND Colour LIKE '%' + @Colour + '%'
       AND Fuel_Type LIKE '%' + @FuelType + '%'
and not exists (select 1 
                from bookings B 
                where H.bookings =B.bookings  
                and @book_date between From_Date and To_Date)

change ypur SP to this :

CREATE PROCEDURE SearchForCars(
    @Make VARCHAR(50), 
    @Model VARCHAR(50), 
    @Age INT, 
    @Transmission VARCHAR(50), 
    @Colour VARCHAR(50),
    @FuelType VARCHAR(50),
    @start_date as Datetime,
    @end_date as Datetime

AS
BEGIN 
    SELECT 
       Make, Model, Registration, Age, Transmission, Body_Type, Colour, 
       Estimated_Value, Mileage, Fuel_Type, Rental_Price_Per_Day 
    FROM 
       HAS_Car
    WHERE 
       Make LIKE '%' + @Make + '%'
       AND Model LIKE '%' + @Model + '%'
       AND Age = Age
       AND Transmission LIKE '%' + @Transmission + '%'
       AND Colour LIKE '%' + @Colour + '%'
       AND Fuel_Type LIKE '%' + @FuelType + '%'
       AND Car_ID NOT IN (select Car_ID from booking_table Where (@start_date>to_date or @start_date<=from_date ) AND (@end_date>=to_date or @end_date<from_date )** 

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM