检查车辆的可用性

时间:2014-03-14 21:45:51

标签: php oracle

我有3张表来登记车辆租赁。

第一个是我在date_out和date_return

中插入请求的地方
Table_Solicitation(Id_Solicitation, date_out, date_return)

其他是我注册车辆的地方

Table_Vehicle(Id_Vehicle, description)

最后表格是否批准

Table_Approval(Id_Approval, Id_Solicitation, Approved, Id_Vehicle)

现在我需要返回给定date_out和date_return的所有可用车辆,但是当我在sql中这些日期为空时我遇到了问题,因为我认为是左连接。

sql是

*select V.Id_Vehicle
from Table_Vehicle V
left join Table_Approval TA on (V.Id_Vehicle = TA.Id_Vehicle)
left join Table_Solicitation TS on (TS.Id_Solicitation= TA.Id_Solicitation)
where 
(to_date('$dateOut', 'dd/mm/yyyy hh24:mi') NOT BETWEEN TS.date_out AND TS.date_return) 
AND
(to_date('$dateReturn', 'dd/mm/yyyy hh24:mi') NOT BETWEEN TS.date_out AND TS.date_return) 
AND
(TS.date_out NOT BETWEEN to_date('$dateOut', 'dd/mm/yyyy hh24:mi') AND to_date('$dateReturn', 'dd/mm/yyyy hh24:mi'))*

现在我有了这些数据

Solicitation 1: from 15/03/2014 13h to 15/03/2014 15h (vehicle 1 is confirmed)
Solicitation 2: from 14/03/2014 13h to 14/03/2014 15h (vehicle 1 is available at the combo box, which is correct).

Solicitation 3: from 14/03/2014 12h to 14/03/2014 14h (vehicle 1 is available at the combo box, but it should not, because conflicts with solicitation 2)

Solicitation 4: from 14/03/2014 12h to 16/03/2014 14h (vehicle 1 is available at the combo box, but it should not, because conflicts with solicitation 1)

我在使用sql中的between子句做错了什么?

1 个答案:

答案 0 :(得分:0)

您说问题是由于日期的空值。你为什么不只是“isnull()”的日期,然后你可以比较。有关isnull的oracle变体,请参阅https://stackoverflow.com/a/3524035/1662973,您可以在其中为其提供随机值,这样您的查询就可以正确比较,即生成日期为1/1/1900。