SQL嵌套基于日期的select语句

时间:2015-01-04 19:27:43

标签: c# sql asp.net sql-server gridview

这是一个小型汽车租赁网站。

我有一个搜索工具,可以搜索CAR表格中的所有汽车,其中包含2个参数:From日期和To日期 - 客户想要的日期租车去。还有下拉框,客户可以从中指定他们想要看哪种类型的汽车。

我目前的返车方法是存储过程:

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;

我还有一个预订表,其中包含car_idcustomer_idto_datefrom_date

在主搜索中,我只想根据客户指定的日期返回尚未预订的车辆。

这涉及将汽车餐桌加入预订表,但我不知道该怎么做。

例如,这是汽车表的一部分:

+--------+----------+-------+---------+---+-----------+-----------+-----------+-------+-------+--------+----+
|      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 |
+--------+----------+-------+---------+---+-----------+-----------+-----------+-------+-------+--------+----+

这是预订表:

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

如果我现在搜索9号车,从2015年2月1日到2015年1月13日租用,那么9号车(奥迪)不应出现在主搜索中,因为它已经有重叠预订

2 个答案:

答案 0 :(得分:0)

试试这个。

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)

答案 1 :(得分:0)

将您的TSP更改为:

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