Reputation: 1202
hi i am trying to get nearest Date here is my code
SELECT schedule_date FROM schedule_table WHERE schedule_date > '2012-06-07' and event_id='23' ORDER BY schedule_date ASC
LIMIT 1
here actual table schedule_date contain 2012-06-07 00:00:00 like this.But i Check '2012-06-07' so
i get output like this 2012-06-07 06:53:57
that mean same date came but i need 2012-06-08 04:53:57
i think here we need to check this condition using like operator
how could i use like operator in this query to get closest date in sql. Thanks for advance.
Upvotes: 0
Views: 896
Reputation: 21810
Query: sort by date, greater than / equal to 2012-06-07, limit 1
the record returned should be your desired row
Note: Do not use LIKE operator. It is unnecessary here because it is meant for searching for text matches within a given string. Which is clearly not what your query is trying to accomplish.
Upvotes: 2
Reputation: 1335
You have a classic case of matching date value with a datetime field type. What you need is the earliest time value within the same date. Possibly your data contains multiple rows with data on same date but different time values. You should use the following:
SELECT schedule_date FROM schedule_table WHERE date(schedule_date) >= '2012-06-07' and event_id='23' ORDER BY schedule_date ASC
LIMIT 1
The date() function is explained in MySQL documentation here.
Upvotes: 1