0

我有一个名为trips的表,您可以在其中使用指定的departure_date和保存行程return_date。车辆保存在车辆表中。我需要查询在特定日期可用的所有车辆,以便如果车辆被选择用于特定旅行,则在返回之前不能再次用于另一次旅行。
我的表结构是这样的

表:行程

id
vehicle_id
departure_date
return_date

表:车辆

id  
plate_no

我尝试使用以下命令进行查询,但它似乎不起作用:

select v.id from vehicles as v
where v.id not in
    (select vehicle_id from trips
     where (departure_date between '2012-10-31 10:41:30' AND '2012-11-06 10:41:38'))
4

1 回答 1

1

尝试这个::

select 
v.id 
from 
vehicles as v 
left join trips t on (v.id=t.vehicle_id)
where 
t.departure_date between '2012-10-31 10:41:30' AND '2012-11-06 10:41:38' 
and t.id is null
于 2012-11-30T19:02:04.100 回答