consult record between two dates in mysql

Asked

Viewed 2,773 times

1

I am trying to make a query in Mysql as follows:

SELECT * FROM `tabela` WHERE `data` >= '2017-03-01' AND `data` <='2017-03-05')

The issue is that even having several records of the day 2017-03-05, the query only returns me records until the day 2017-03-04. I need to have the return of the 2017-03-05 day records without having to use a date above.

Someone has a solution to help me?

2 answers

2


Try to use with between

SELECT * FROM `tabela` WHERE `data` between '2017-03-01' AND '2017-03-05'

If using Datetime, set the start date to 00:00:00 and end date to 23:59:59 to consider the entire two days

SELECT * FROM `tabela` WHERE `data` between '2017-03-01 00:00:00' AND '2017-03-05 23:59:59'
  • That was it. Thanks!

  • @Brunooliveira if the answer of Antonio helped and solved the problem pff accept: http://meta.pt.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta below the arrows above/below the left of the answer

  • A question: is it a correct practice when Datetime do this? Why I do, but I find it strange... Don’t you have a better solution to count all day, like a function?

0

This query only checks in the interval until the day 05/03/2017 at 00:00hrs. Try to put in the last parameter the full date, example:

SELECT * FROM tabela WHERE data >= '2017-03-01' AND data <='2017-03-05 23:59:00'

Taking into account that the field is of the type datetime or timestamp.

I suggest to better optimize your consultation by removing equality operators.

SELECT * FROM tabela WHERE data BETWEEN '2017-03-01 00:00:00' AND '2017-03-05 23:59:00'

  • Thanks my friend! Solved.

  • For nothing. Arrange!

  • The community has given much downvote on account of the use of consultations between such dates. Behold

Browser other questions tagged

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