Rejoanul Alam
Rejoanul Alam

Reputation: 5398

mysql DATETIME BETWEEN query not working as expected

I have following data which clearly showing data available between date used in query enter image description here

My query

SELECT * FROM dashboard WHERE added BETWEEN "2018-05-08 10:32:32" AND "2018-05-08 10:28:30"

but This query returning empty row set. What going wrong. I have exported this table from production server & imported in development server. addedfield is DATETIME. Please help

Upvotes: 0

Views: 68

Answers (2)

Paul Maxwell
Paul Maxwell

Reputation: 35613

The first boundary must be lower than the second.

Try reversing the comparison values. E.g

SELECT * FROM dashboard 
WHERE added BETWEEN "2018-05-08 10:28:30" 
      AND "2018-05-08 10:32:32"

The reason behind this is due to the fact that the first value is compared using >= then the second value compared using <= thus the second value must be greater than the first or nothing is returned.

Upvotes: 3

apomene
apomene

Reputation: 14389

the clause: BETWEEN "2018-05-08 10:32:32" AND "2018-05-08 10:28:30" is correctly returning an empty set since from is after until. Did you mean:

BETWEEN   "2018-05-08 10:28:30" AND "2018-05-08 10:32:32" 

Upvotes: 1

Related Questions