事件表
这是我的事件表,我需要的是从当前日期获取最接近的日期值。
当前表
--------------- --------------------------------------
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
------------------------------------------------------
我的预期结果
--------------- --------------------------------------
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
------------------------------------------------------
到目前为止我尝试了什么
select * from events where CURDATE() < Conversion_Rate
我需要的是,我需要获得离当前日期最近的日期以及转换率!='0'。如何在mysql中获得预期结果?
如果我理解正确,您希望获取当前日期之前的最新记录,该记录与当前日期及以后的所有其他记录具有非零转换率。
如果是这样,UNION
可能会对您有所帮助,如以下示例所示:
(
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
)
试试这个
SELECT *
FROM Table
WHERE Conversion_Rate != 0
ORDER BY ABS(DATEDIFF(future_dates, NOW()))
LIMIT 10
或
SELECT *
FROM table
WHERE Conversion_Rate != 0
ORDER BY ABS(now() - future_dates) DESC
limit 10
Future_date应为'13/08/2015'。此记录应在今天显示。我想你可能想要这个查询。
select * from events where CURRENT_DATE - INTERVAL 5 DAY <=future_dates and Conversion_Rate != 0
select *
from events
where Conversion_Rate != '0'
order by ABS(DATEDIFF(CURDATE(),future_dates )) asc