I used this query:
SELECT
tavolo.ID_Tavolo
FROM
tavolo
LEFT JOIN prenota
ON tavolo.ID_Tavolo = prenota.ID_Tavolo
WHERE
tavolo.ID_Tavolo NOT IN
(SELECT
Prenota.ID_Tavolo
FROM
prenota)
OR tavolo.ID_Tavolo IN
(SELECT
prenota.ID_Tavolo
FROM
prenota
WHERE
Data_Pren != '2020-06-04'
OR Data_Pren = '2020-06-04'
AND Ora_Pren NOT BETWEEN '19:00:00' AND '20:30:00'
AND ADDTIME(Ora_Pren, "1:30:00.000000") NOT BETWEEN '19:00:00' AND '20:30:00')
ORDER BY Tavolo.ID_Tavolo
It worked when there was no data in the database but, after creating 3 reservations (I have 3 tables), it started using the same ID every time (7) not checking the date.
Theese are my tables:
Prenota table (structure)
Prenota table (data)
Tavolo table (structure)
Tavolo table (data)
(The query works by checking:
1) If the ID doesn't exist in the table "prenota" give me the ID;
2) If the ID already exists and the date is different OR the date is the same but the hour IS NOT BETWEEN the registration hour and +1h30m AND adding 1h30m to check if all the tables are still unavailable during that hour.
Here's a screen of me trying the query:
I simply want to know how to make a reservation for a table working by checking:
1) The ID of the table
2) The Date of the already reserved tables
3) The Time of the already booked tables
4) Make a reservation if a "gap" is found checking the time chosen by the user and the time +1h30m (the max time that you can stay at the table)
Here's the link to try it: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=87563a5b3f14efee1ffc535e14675f06