Mysql select data by period

Asked

Viewed 113 times

2

I’m trying to select data that starts on one date and ends on another, I’m doing it this way:

SELECT * FROM vendas WHERE feita_em >= '2019-10-10' AND feita_em <= '2019-10-11'

And this is the result:

Resultado da query

But I have data on the 11th, but they are not being shown in the query, only the 10th, and I need that data to produce my reports.

2 answers

3


The factor is that you record datetime and compares with date a solution is to use the function date() of the bank as follows:

SELECT * FROM vendas WHERE date(feita_em) >= '2019-10-10' 
                       AND date(feita_em) <= '2019-10-11'

It will already solve because regardless of time the date has it will only consider the date.

Another way would be to tailor the time in this comparison where the initial date begins to 00:00:00 and the end date of the 23:59:59 ai being unnecessary to convert the date stored in the bank, example:

SELECT * FROM vendas WHERE feita_em >= '2019-10-10 00:00:00' 
                       AND feita_em <= '2019-10-11 23:59:59'

With BETWEEN and the adjustment that has already been explained:

SELECT * FROM vendas WHERE date(feita_em) BETWEEN '2019-10-10' AND '2019-10-11' // ou
SELECT * FROM vendas WHERE feita_em BETWEEN '2019-10-10 00:00:00' AND '2019-10-11 23:59:59'
  • 1

    Question nothing to do, in this situation which would be the most effective ?

  • 1

    @Scrapbench your question is good yes, would have to analyze the two SQL One goes through conversion to the other won’t depend on the scenario. Sincerity has to check the plan of execution of the two, for little information I think (achismo, no test) that has no significant differences

0

I think the problem is that you treat datetimes like Dates tries this

SELECT * FROM vendas  WHERE feita_em BETWEEN '2019-10-10' AND '2019-10-11';
  • syntax error

  • sryja changed I forgot to take the =>

  • Same result

  • Error Syntax? or without shopping on the 11th ?

Browser other questions tagged

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