MySQL: Fetch nearest date

Discussion in 'MySQL' started by cancer10, Aug 28, 2008.

  1. #1
    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())
    Code (markup):
    Even a search in Google didnt help :(


    Please help.

    Thanx
     
    cancer10, Aug 28, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You could try:

    SELECT YourDateField FROM yourTable ORDER BY yourDateField DESC LIMIT 1
     
    jestep, Aug 28, 2008 IP
  3. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Try this
    select YourDateField from yourTable 
    ORDER BY yourDateField DESC LIMIT 1
    Code (markup):
     
    mwasif, Aug 28, 2008 IP
  4. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #4
    close call
     
    mwasif, Aug 28, 2008 IP
  5. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Tried that as well, no luck :(
     
    cancer10, Aug 28, 2008 IP
  6. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6

    What is that?
     
    cancer10, Aug 28, 2008 IP
  7. zodiac

    zodiac Peon

    Messages:
    2,661
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    0
    #7
    try:
    
    select `YourDateField` from yourTable ORDER BY `yourDateField` DESC LIMIT 1
    
    PHP:
     
    zodiac, Aug 29, 2008 IP