I have a problem: I have a table of delivery times with times for each period. Ex .: From 14:00 às 22:30
the delivery time is 40min
. I can get this with SELECT
using BETWEEN
.
When arriving in a delivery period as Das 22:00 às 02:00
delivery time is 55min
SELECT
simple with BETWEEN
is not solving the problem.
hora_inicio: 22:00
hora_fim: 02:00
tempo: 00:55
Let's suppose it's now 11:00 PM so it gets 23:00 ENTRE 22:00 E 02:00
, only that does not hit because 02:00
is already the next day, right?
The SQL statement used in the 'easy part' was:
SELECT tempo FROM tempo_entrega WHERE id_restaurante = 12 AND CURTIME() BETWEEN hora_inicio AND hora_fim
Following the line of reasoning of @JoaoRaposo look:
id_restaurante:12
hora_inicio: 22:00:00
hora_fim: 02:00:00
tempo: 00:40:00
id_restaurante:12
hora_inicio: 02:30:00
hora_fim: 16:30:00
tempo: 00:25:00
I applied the following statement (I believe I have adapted correctly in Mysql, I do not have knowledge of IF ELSE within SQL:
SELECT tempo FROM tempo_entrega WHERE id_restaurante = 12 AND CURTIME() BETWEEN hora_inicio AND hora_fim OR id_restaurante = 12 AND CURTIME() BETWEEN hora_inicio AND '23:59:59' OR id_restaurante = 12 AND CURTIME() BETWEEN '00:00:00' AND hora_fim
And I had a return: tempo: 00:25:00
So far, everything perfect ... masss ... how much I changed the "CURTIME ()" by "23:30:00" the return was:
tempo: 00:40:00
tempo: 00:25:00
I imagine that because I have used the OR, it has taken both ways ... but using the AND instead of the OR does not return either one or the other. :