简体   繁体   English

如何获取日期时间内没有预订或可用的所有汽车

[英]How to get all cars which is not booked or available between a datetime

I have two table first is cars and second is booking and my search fields is 我有两个表,第一个是汽车,第二个是预订,我的搜索字段是

  • Location 位置
  • type 类型
  • pick datetime 选择日期时间
  • drop datetime 删除日期时间

so please let me know how to get available cars which is not booked now between a date time if car booked then it should not be in list. 因此,请让我知道如何获取在某个日期时间之间当前未预订的可用汽车(如果已预订汽车,则该汽车不应在列表中)。 list should be come only available cars by search. 该列表应仅是通过搜索可获得的汽车。

Cars Table 汽车表

----------------------------------------------
id | name | type | location | pincode
----------------------------------------------
 1    A    plain   delhi       110093
 2    B    plain   deldi       110093
 3    C    plain   deldi       110093
 4    D    plain   deldi       110093
 5    E    plain   deldi       110093

Booking Table 预订表

id | car_id | user_id  |      pick_date    | drop_date
-----------------------------------------------------------------------------
 1    1         1        2016-05-10 13:00:00  2016-05-12 16:00:00    
 2    1         1        2016-05-12 18:00:00  2016-05-12 23:00:00

In the your_startdate and your_enbdate you should insert your date range 在your_startdate和your_enbdate中,您应该插入日期范围

Select cars.* 
from cars
Left join booking on cars.id=booking.id and pick_date between your_startdate and your_enddate
Where booking.id is null

Use this query... 使用此查询...

Find the cars id's from booking that are booked on given dates and then find all the cars from cars table where id not equal to booked cars.. 从给定的日期预订中查找预订的汽车ID,然后从汽车表中查找ID不等于预订汽车的所有汽车。

SELECT * FROM cars WHERE id NOT IN (SELECT car_id FROM booking 
                                    WHERE pick_date >= '2015-06-03'
                                    AND drop_date <= '2015-06-05'
                                   );

According your search variables, first select car_id from Booking table between pick datetime and drop datetime. 根据您的搜索变量,首先从“预订”表中选择“选择日期时间”和“放置日期时间”之间的car_id

Now you have a list of car_id which is booked between that date and time. 现在,您有了在该日期和时间之间预订的car_id列表。

Use a loop and make a select query from cars table where id != car_id and location and type will be as your search variables. 使用循环并从cars表中进行选择查询,其中id != car_id ,位置和类型将作为您的搜索变量。

use between query and know which car are booked and also know the specific time car available are not . 在查询之间使用并知道预定了哪辆汽车,还不知道可用的具体时间。

given_time_here_search_time = "2016-05-11 15:00:00"; named_time_here_search_time =“ 2016-05-11 15:00:00”; //user input //用户输入

query should be something like this 查询应该是这样的

SELECT * FROM cars
WHERE id NOT IN (SELECT car_id 
FROM booking 
WHERE given_time_here_search_time  
between pick_date
AND drop_date ) and location='delhi' and type='plain';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM