douyan2470 2015-08-10 10:11
浏览 58
已采纳

如何从mysql中获取当前日期的最近日期值

Events Table

This is my events table what i need is i need to get the nearest date values from current date.

Current Table

--------------- --------------------------------------
events          future_dates      Conversion_Rate
--------------- --------------------------------------
One 1            01-08-2015        1000
One 1            03-08-2015        1000
One 1            06-08-2015        1000
One 1            07-08-2015        1000
One 1            10-08-2015        0
Two 1            13-08-2015        0
Two 1            14-08-2015        0
Two 1            16-08-2015        0
------------------------------------------------------

My Expected Result

--------------- --------------------------------------
events          future_dates      Conversion_Rate
--------------- --------------------------------------
One 1            07-08-2015        1000
One 1            10-08-2015        0
Two 1            13-08-2015        0
Two 1            14-08-2015        0
Two 1            16-08-2015        0
------------------------------------------------------

What i have tried so far

select * from events where CURDATE() < Conversion_Rate

What i need is i need to get the nearest date from current date and where conversion rate != '0'.How can i get the expected result in mysql ?

  • 写回答

4条回答 默认 最新

  • dongliulu1122 2015-08-10 10:40
    关注

    If I understand correct you want to fetch a latest record before current date which has a non zero conversion rate along with all other records from current date and later.

    If so, a UNION may help you as shown in the following example:

    (
    select * from table_name 
     where conversion_rate <> 0 
       and future_dates < current_date
     order by future_dates desc limit 1
    ) -- this fetches pre cur date recs
    union
    ( -- this fetches cur and post cur date recs
    select * from table_name 
     where future_dates >= current_date
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理