表里有两个字段,一个id (int),一个diskdate (datetime)
怎么样得到离今天最近的id
【不需要考虑diskdate大于今天的情况】

解决方案 »

  1.   

    order by 一下diskdate desc再取top 1
      

  2.   


    select id from table
    where abs(datediff(hour,diskdate,getdate()) = (
       select min(abs(datediff(hour,diskdate,getdate())) from table
       )
      

  3.   

    4楼好复杂~
    sql函数不太清楚
    datediff(hour,diskdate,getdate()) 是什么意思啊?
    abs()什么意思?
    min我知道是最小值的意思吧~
      

  4.   

    ABS(): 取绝对值
    datediff():求两个时间的时间间隔,其中
      hour: 间隔单位为小时, 其他单位包括:year, month, week, day, hour, minute, second, ..
      deskdate: 你的字段数据
      getdate(): 当前时间