Trying to find vehicles which are free between 2 variable dates

前端 未结 2 867
遇见更好的自我
遇见更好的自我 2021-01-26 09:07

I\'m trying to find out which, if any, vehicles are free between a pair of user input variables as dates.

Each vehicle, which is being used for a trip, has a start and e

相关标签:
2条回答
  • 2021-01-26 09:33

    Something like this, although I may have got the >= mixed up. Assuming a vehicle table and vehicle_id being the primary key/foreign key:

    This method has the added benefit of showing vehicles that haven't yet been booked for a trip.

    Select
      v.*
    From
      vehicle v
    Where
      Not Exists (
        Select
          'x'
        From
          trips r
        Where
          r.vehicle_id = v.vehicle_id and
          :return >= r.departure and
          :departure <= r.return
    )
    
    0 讨论(0)
  • 2021-01-26 09:41

    You're using AND, which implies both conditions have to be true. You need to use OR instead:

    select vehicle_registration
      from trips
     where departure not between :departure and :return
        or return not between :departure and :return
    

    On a side not, as indicated by the syntax highlighting, return is a bad name from a column name. It's a reserved word in Oracle and you should avoid it if you can.

    0 讨论(0)
提交回复
热议问题