MySQL日期范围查询,查询前后几天
注意:前N天当天内指的是仅仅那一天内的,前N天内指的是1 2 3 … n这几天所有的数据,可自行检验
- 今天(前0天)当天的数据
select * from 表名 where to_days(now())(表中的时间字段) = to_days(now())
或
select * from 表名 where to_days(now())- to_days(now())(表中的时间字段)=0
- 昨天(前1天)当天的数据
select * from 表名 where to_days(now())- to_days(now())(表中的时间字段)=1
- 前天(前2天)当天的数据
select * from 表名 where to_days(now())- to_days(now())(表中的时间字段)=2
因此:
- 前n天当天内的数据
select * from 表名 where to_days(now())- to_days(now())(表中的时间字段)=n
- 前n天 内 的数据:(=改成<=即可)
select * from 表名 where to_days(now())- to_days(now())(表中的时间字段)<=n