user3680687
user3680687

Reputation: 31

Mysql between query for two date is not working properly

I have a table that contain two datetime field. So when I going to search anything betwwn two dates then it do not show exact result. It suffering me more when I want to see only one day search result. My code is

SELECT * FROM item WHERE production_date BETWEEN $a AND $b

Upvotes: 2

Views: 5222

Answers (2)

Eduard
Eduard

Reputation: 3576

You need to CAST your production_date as DATE like this, otherwise BETWEEN will get exclusive instead inclusive.

SELECT * FROM item WHERE DATE(production_date) BETWEEN $a AND $b

I'm not sure if your column has a time component, truncate it out with DATE(column) but if it does, this should do the trick.

Upvotes: 7

Md Riad Hossain
Md Riad Hossain

Reputation: 291

You are using datetime as the type for your field. Change it to only date and it will works fine!

Or you can use '2014-05-27 00:00:00' as start date and '2014-05-28 23:59:59' as your end date. Means add some extra string with your variable.

Upvotes: -1

Related Questions