Check duplicate car reservation

I am writing a simple reservation program for car rental (school assignment). My friend and I are trying to make the system a little more advanced than the task dictates, but we have some problems that we hoped that you could help us.

The idea is that you can reserve a certain type of car, and when you receive the car, it will be one of these types (you do not reserve a specific car, as our purpose dictates, but only the type). Only one customer can have a car on a specific date. As reservations are ticking, we must make sure that we do not pick up more cars of each type than ours. Reservations are mostly stored with start date, end date and vehicle type.

If at the moment we are ignoring the type of car (say, we have only one type), then reservations may look like this:

1/12  2/12  3/12  4/12  5/12  6/12  7/12
|-------------------|
                    |-----------------|
                                |-----|
|-------|
                          |-----------|
|-------------|

If you rent only three cars, you can rent a car from 3/12 to 5/12, as there are only 2 parking lots throughout the day. But how do we know that? Is it necessary to check every date and quantity () of the number of reservations that cover this date?

And what if someone reserved the car on 4/12, then 3/12 and 5/12 would still have only 2 reservations, but 4/12 would have 3.

- - , , , ? ( , , ( , ). , , , ...)

, , . , , :)

  • ,
+3
3

" - - , , , ? ( , ",

, . , , , . " ". , .

, 4/12-6/12 4/12-5/12 ( ) 5 ​​/12-6/12 ( ). , . ( , 7/12-19/12 .

, : , , ( , ).

+1

, :

       1/12  2/12  3/12  4/12  5/12  6/12  7/12
Car1:  |-------------------|
Car2:                      |-----------------|
Car3:  |-------|     |-----------|     |-----|                              
Car4:  |-------------|

car

| id | type | registration |
| 1  | 1    | HH1111       |
| 2  | 1    | HH3333       |
| 3  | 2    | HH77         |
| 4  | 3    | DD999        |

reservation

| car_id | date_from  | date_to    |
| 1      | 2013-12-01 | 2013-12-04 |
| 2      | 2013-12-04 | 2013-12-07 |
| 3      | 2013-12-01 | 2013-12-02 |
| 3      | 2013-12-03 | 2013-12-05 |
| 3      | 2013-12-06 | 2013-12-07 |
| 4      | 2013-12-01 | 2013-12-03 |

, ,

2013-12-05 2013-12-06

" , , " brillian mysql :

select * from car  where not exists ( select * from reservation 
where car.id = reservation.car_id AND
date_from < '2013-12-06' AND
date_to > '2013-12-05' )  
+1

My table has a date and time, how can I check the reservation?

0
source

Source: https://habr.com/ru/post/1777226/


All Articles