Get closest date from MySQL table

前端 未结 3 779
旧巷少年郎
旧巷少年郎 2020-12-11 22:39

I need to get the closest date to current date from a MySQL table.

This is my table:

id        | date          | name
1         | 2012-10-29    | tes         


        
相关标签:
3条回答
  • 2020-12-11 22:53
    SELECT * FROM `your_table` WHERE ABS(DATEDIFF(`date`, NOW()));
    

    Returns:

    '1', '2012-10-29 00:00:00', 'test'
    
    0 讨论(0)
  • 2020-12-11 22:59
    SELECT 
      * 
    FROM 
      your_table 
    ORDER BY 
      ABS(DATEDIFF(NOW(), `date`))
    LIMIT 1
    
    0 讨论(0)
  • 2020-12-11 23:03
    select top 1 date from table
    where date > now()
    order by date desc
    
    0 讨论(0)
提交回复
热议问题