2
I have a problem: I have a table of delivery periods with times for each period Ex.: Das 14:00 às 22:30
the delivery time is 40min
. I can get it with a SELECT
using BETWEEN
.
When you arrive at a delivery period like Das 22:00 às 02:00
the delivery time is 55min
the SELECT
simple with the BETWEEN
is not solving the problem.
hora_inicio: 22:00
hora_fim: 02:00
tempo: 00:55
Let’s say it’s now 11:00 23:00 ENTRE 22:00 E 02:00
, only it doesn’t add up because 02:00
It’s 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 only:
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 instruction (I believe I have adapted correctly in Mysql, I have no 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 got back: tempo: 00:25:00
So far, everything perfect... masss... how much I traded "CURTIME()" for "11:30 am" the return was:
tempo: 00:40:00
tempo: 00:25:00
I imagine, because I used the OR he took both forms... but using the AND in place of the OR does not return either. :(
Post the code you are using to give SELECT in both 40min and 55min time
– Maicon Carraro
Can you use the date too? or only' has even the hours/minutes?
– Joao Raposo
The type used for these two fields is
DATETIME
?– Paulo Rodrigues
I edited up there with the SQL I used, the fields hora_start and hora_end, are of the type TIME and I do not use the date in this case, because this will be valid for any day of the week. but I accept suggestions.
– Stedile93
The easiest solution would be to separate this entry "22:00 until 02:00" in 2 entries: 1 - "22:00 until 23:59:59" 2- "00:00 until 02:00", so avoid the problem of the BETWEEN dates, or you will have to implect this logic in the SQL input
– Joao Raposo
I think this will end up being the best shape even John Fox. The only annoying thing is going to be having to treat these duplicated entries so the user can remove them or change them. That was the main reason I persisted with this more 'dry' form. But you commented on implementing in the SQL statement, could give me a help with this, I understand a little but I am not as deep with SQL, for lack of time and specific needs I ended up learning only what was needed =/
– Stedile93