Mysql Query:从特定日期识别可用车辆

Posted

技术标签:

【中文标题】Mysql Query:从特定日期识别可用车辆【英文标题】:Mysql Query: Identify available vehicle from specific dates 【发布时间】:2012-11-30 19:00:11 【问题描述】:

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

表格:旅行

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'))

【问题讨论】:

【参考方案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

【讨论】:

以上是关于Mysql Query:从特定日期识别可用车辆的主要内容,如果未能解决你的问题,请参考以下文章