【问题标题】:Between query within a database在数据库内的查询之间
【发布时间】:2016-12-13 03:34:05
【问题描述】:

我多年来一直试图解决这个问题,但仍然没有运气。

我正在尝试建立一个车辆数据库,我可以在其中检查我输入的日期的车辆可用性,以便人们可以出租它们。例如,他们想在 2016 年 11 月 5 日至 2016 年 11 月 20 日期间租车。

我已经能够将查询连接回车辆表,但是当我测试查询时,它要么给我所有被列为可用的车辆,要么没有。

我已经包含了表格和关系,因为我认为每个表格之间的表格可能会导致我出现问题。

非常感谢任何修复此代码的建议。

Relationships Tables

SELECT *
FROM Vehicles
WHERE Vehicles.vehicle_id NOT IN 
        (
        SELECT distinct Booking.[vehicle id]
        FROM Booking
     WHERE (
    [Enter Start Date] BETWEEN booking.start_rent_date
        AND booking.end_rent_date
    )
OR (
    [Enter End Date] BETWEEN booking.start_rent_date
        AND booking.end_rent_date
    )
 );

【问题讨论】:

    标签: sql database ms-access-2016


    【解决方案1】:

    我认为这是left join

    select v.*
    from vehicles v left join
         bookings b
         on v.vehicle_id = b.vehicle_id and
            b.start_rent_date <= @EnterEndDate and
            b.end_rent_date >= @EnterStartDate
    where b.vehicle_id is null;
    

    如果在期望的结束日期之前开始并在期望的开始日期之后结束的租赁,则车辆不是免费的。 left join 不包括这些。您也可以使用 NOT EXISTSNOT IN 来实现等效逻辑。

    编辑:

    哦,您正在使用 MS Access 。 . .

    select v.*
    from vehicles as v left join
         (select b.*
          from bookings as b
          where b.start_rent_date <= @EnterEndDate and
                b.end_rent_date >= @EnterStartDate
         ) as b
         on v.vehicle_id = b.vehicle_id
    where b.vehicle_id is null;
    

    【讨论】:

    • 我收到一个缺少运算符的语法错误,不知道为什么会发生这种情况
    • 一开始它对我有用,但现在当我尝试它时,所有车辆都显示出这是如何发生的任何想法
    • 没关系,输入日期的方式不对,伙计...非常感谢,我花了几个小时试图解决这个问题,我再次感谢你
    【解决方案2】:
    SELECT  *
    FROM    Vehicles
    WHERE   Vehicles.vehicle_id NOT IN (
                                        SELECT distinct D1.[vehicle id]
                                        FROM Booking D1
                                        WHERE ( DATEDIFF(DAY, D1.start_rent_date, @Enter_Start_Date)>=0
                                                And DATEDIFF(DAY, D1.end_rent_date, @Enter_Start_Date)<=0
                                                )
                                                OR 
                                                (
                                                DATEDIFF(DAY, D1.start_rent_date, @Enter_End_Date)>=0
                                                And DATEDIFF(DAY, D1.end_rent_date, @Enter_End_Date)<=0
                                                )
                                                OR 
                                                (
                                                DATEDIFF(DAY, D1.start_rent_date, @Enter_Start_Date)<=0
                                                And DATEDIFF(DAY, D1.end_rent_date, @Enter_End_Date)>=0
                                                )
                                        );
    

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多