query record between two dates in mysql [duplicate]

1

I am trying to query MySql as follows:

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

The issue is that even though I have multiple records from 2017-03-05, the query only returns me records until 2017-03-04. I need to get the records back from 2017-03-05, without having to use a date above.

Does anyone have a solution to help me?

    
asked by anonymous 15.03.2017 / 21:36

2 answers

2

Try to use with between

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

If you are using DateTime, set the start date to 00:00:00 and end date as 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'
    
15.03.2017 / 21:39
0

This query only checks in the range until 03/03/2017 until 00: 00hrs. Try to put in the last parameter the complete date, example:

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

Considering that the field is of type datetime or timestamp .

I suggest optimizing your query better by removing equality operators.

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

    
15.03.2017 / 21:42