如何在MYSQL中选择Interval和1下一条记录?

时间:2015-05-25 19:35:25

标签: mysql

SELECT * FROM $dbtable WHERE date>=NOW()- INTERVAL 1 DAY

我需要选择最后一天的记录,还需要更早一些。我怎样才能实现这一目标?

2 个答案:

答案 0 :(得分:0)

/* last day's records*/    
SELECT * FROM $dbtable WHERE date BETWEEN DATEDIFF(NOW() - 1 days) AND NOW()

UNION

/* one record from yesterday and 1000 days ago */
SELECT * FROM $dbtable WHERE date BETWEEN DATEDIFF(NOW() - 1000 days) AND (NOW() - 1 days) 
ORDER BY date DESC
LIMIT 1    

答案 1 :(得分:0)

如果你担心而不是时间,那么这可能是你想要的:

SELECT *
FROM $dbtable
WHERE date >= date_sub(CURDATE(), interval 2 day);

如果您使用NOW(),您将拥有时间组件(这似乎不合适)。