Comparison of Mysql hours

Asked

Viewed 662 times

1

I have an agenda where I register an appointment, that commitment has Hora Inicial and Hora Final. However now when I register a new appointment I have to make a check to see if the hours are not between the hours already registered. Example:

I have an appointment with Home Time 13:00 and Final Hour: 14:00 On today’s date. How do I check this time period to not let anything be registered in this interval of 13:00 as 14:00?

  • You need to do this via Mysql or java?

  • I need to do Mysql by spending two hours as parameter.

  • I thought about doing it this way: Whenever I go to save an appointment I make a query in the bank spending two hours as parameter, starting time and end time. From there in the bank I make a check to see if the hours passed as parameter are within the period of some already saved commitment. But I do not know if it is possible to do this in bank rsrs

  • 1

    As I said in the other question I’m very busy these days, you can do something with the command BETWEEN mysql where you test something during an interval that in case would be between 13 and 14. If you do not get anything at home put an example,

  • I’ll try to use the BETWEEN, thanks for sending the tip.

1 answer

3


To find records between two dates in Mysql, you can use the function BETWEEN.

If it’s a column like datetime:

SELECT *
FROM `minhaTabela`
WHERE `campoDatetime` BETWEEN '2015-07-10 13:00:00' AND '2015-07-10 14:00:00';

If it’s a column like time:

SELECT *
FROM `minhaTabela`
WHERE `campoTime` BETWEEN '13:00:00' AND '14:00:00';

If the query returns 1 or more results, then you know that commitments already exist.

  • Opa worked right, I did it like this:SELECT * FROM db_agenda.tbl_compromissos Where '15:00' BETWEEN hora_inicio And hora_fim. Now one last question, how do I pass a parameter instead of this 15:00 in java code?

  • I tended in a way that didn’t work out so well: tabela = consulta.executeQuery("SELECT cod_compromisso, nome_beneficiario, compromisso FROM tbl_compromissos Where hora_inicio='"+horaInicio+"' BETWEEN hora_inicio And hora_fim");

  • Oops, I got it here too. Thanks

  • That one 15:00 is coming as parameter? if you are can pass for example psmt.setDate(1, agenda.getHorarioEscolhido());, where the number 1 indicates the first parameter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.