I am new to the forum, it has helped me so much so far. But now I can not find a solution for the following:
I have a table in MySQL called "events" and another one called "vehiculos", the application I'm designing is for reserving vehicles with PHP and MySQL.
The eventos
table contains: id, start_date, end_date, start_time, end_time, user, vehicle
Table vehiculos
contains: id, registration, model, enabled.
When a person is going to make a reservation, the system has to assign a vehicle automatically (while it is enabled), but for this I have to know that it is not reserved for events in the range of dates and times that the user wants.
I have the following code:
SELECT vehiculos.matricula
FROM reservas.vehiculos WHERE vehiculos.matricula NOT IN (
SELECT vehiculo FROM reservas.eventosv
WHERE '$aux_ini' >= CONCAT(fecha_ini,' ',hora_ini)
OR '$aux_fin' <= CONCAT(fecha_fin,' ',hora_fin)
) AND habilitado='S' AND zona='Zona1' order by id asc LIMIT 1
Where $ aux_ini and $ aux_fin is start date + start time concatenated and end date + end time concatenated.
It worked correctly until the reservations of all vehicles have been made, now you can not reserve any vehicle (even if the dates are different) because it ignores the second WHERE
(that of NOT IN
) since only let me select a field. Any solution?
Thanks in advance.
Greetings.
EDIT:
Doing it just like that, it returns all the license plates that are in vehicles, because there are already reservations of those vehicles on those dates, when what I'm looking for is that I return a license that is not between those dates, so there is overlap I do not understand
SET @f_ini = '2018-12-12';
SET @h_ini = '12:00';
SET @f_fin = '2018-12-14';
SET @h_fin = '17:00';
SET @ini = '2018-12-12 08:00:00';
SET @fin = '2018-12-14 08:00:00';
SELECT vehiculos.matricula
FROM reservas.vehiculos WHERE NOT EXISTS(
SELECT * FROM reservas.eventosv
WHERE eventosv.vehiculo = vehiculos.matricula
AND ( (CONCAT(@f_ini,' ',@h_ini) > @ini AND CONCAT(@f_fin,' ',@h_fin) < @fin)
OR (CONCAT(@f_fin,' ',@h_fin) > @ini AND CONCAT(@f_ini,' ',@h_ini) < @ini)
OR (CONCAT(@f_fin,' ',@h_fin) > @ini AND CONCAT(@f_fin,' ',@h_fin) < @fin)
)
) AND habilitado='S' AND zona='Zona1' order by id asc LIMIT 1