我试图从DATE列获取所有行 从10天前到今天的价值
我试图不理会为什么这种语法不起作用:
select * from table WHERE date BETWEEN NOW() AND NOW() - INTERVAL 10 DAY ORDER BY date
答案 0 :(得分:4)
使用between
select *
from table
WHERE date BETWEEN NOW() - INTERVAL 10 DAY and NOW()
ORDER BY date
答案 1 :(得分:0)
问题与NOW()
函数无关,但与BETWEEN
运算符无关,必须首先指定较低的时间戳:
where date between now() - interval 10 day and now()
但是,根据您的要求,您可能希望使用此:
where date between current_date() - interval 10 day and current_date()
或只是
where date>=current_date() - interval 10 day
now()
返回包含日期和时间信息的timestamp
,而current_date()
仅返回没有时间信息的当前日期。如果日期只是一个date
列,没有时间信息,使用now() - interval 10 day
您将获得最新的9天而不是最新的10天。