在WEB编程中,通常需要查询一天、一周或一个月内的数据记录,贴几条轻松获取指定时间内数据记录的SQL语句:
查询一天:
select * from table where to_days(column_time) = to_days(now()); select * from table where date(column_time) = curdate();
查询一周:
select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY)<= date(column_time);
查询一个月:
select * from table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH)<=date(column_time);
简要说明:
to_days(date)给定一个日期, 返回一个天数 (从年份0开始的天数 );DATE_SUB(date,INTERVAL expr type)函数从日期减去指定的时间间隔。column_time参数表示每条记录对应的日期时间字段。