Click to See Complete Forum and Search --> : MySQL: Fetch nearest date


cancer10
08-28-2008, 08:32 AM
Hey,

How do I get the nearest date/time row populated using sql?

The datatype for this date field is "datetime"

I tried the folowing, but does not work properly

select YourDateField from yourTable where yourDateField <= date(now())

Even a search in Google (http://www.google.co.in/search?hl=en&safe=off&client=firefox-a&rls=org.mozilla:en-US:official&q=mysql+fetch+nearest+date&start=0&sa=N) didnt help :(


Please help.

Thanx

Phill Pafford
08-28-2008, 09:46 AM
Try


SELECT YourDateField
FROM yourTable
ORDER BY YourDateField DESC
LIMIT 1

cancer10
08-28-2008, 09:55 AM
Tried that as well, no luck :(

chazzy
08-28-2008, 09:58 AM
maybe you should clarify with what you mean by "no luck" or "doesn't work" are you getting the wrong data? what are you expecting? maybe if you give us some sample data, what you're currently getting and what you expect to get we can help you write the query.

Phill Pafford
08-28-2008, 10:04 AM
hmm,

What does the data look like for the datetime field?

maybe try this


SELECT YourDateField
FROM yourTable
WHERE yourDateField <= NOW()


What would be the expected results?

Please show some of the data and what the return is that you would like