Dhanasekaran
Dhanasekaran

Reputation: 107

<= displays records of < in mysql query

In MySQL, I am using the following query to get records having the UpdatedAt field in between 2015-02-02 and 2015-02-06

SELECT `TotalPrice`,`UpdatedAt` FROM `price` WHERE `UpdatedAt`>= '2015-02-02' AND `UpdatedAt`<='2015-02-06' ORDER BY `UpdatedAt` DESC  

And this is displaying UpdatedAt from 2015-02-05 to 2015-02-02. And the table has records for 2015-02-06 also. Kindly indicate the mistake i have done.

Upvotes: 0

Views: 38

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269913

My guess is that UpdatedAt has a time component. Here is an alternative where clause:

WHERE `UpdatedAt` >= '2015-02-02' AND `UpdatedAt` < '2015-02-07'

You might find this more readable as:

WHERE `UpdatedAt` >= '2015-02-02' AND
      `UpdatedAt` < DATE_ADD('2015-02-06', INTERVAL 1 DAY)

What you should not get in the habit of doing is:

WHERE DATE(UpdatedAt) >= '2015-02-02' AND DATE(UpdatedAt) <= '2015-02-06'

Although technically correct, this prevents the use of indexes.

Upvotes: 4

Related Questions