1 select * from `article` where date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d');
或者:
1 select * from `article` where to_days(date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d')) = to_days(now());
假设以上表的add_time字段的存储类型是DATETIME类型或者TIMESTAMP类型,则查询语句也可按如下写法:
查询今天的信息记录:
1 select * from `article` where to_days(`add_time`) = to_days(now());
查询昨天的信息记录:
1 select * from `article` where to_days(now()) – to_days(`add_time`) <= 1;
查询近7天的信息记录:
1 select * from `article` where date_sub(curdate(), INTERVAL 7 DAY) <= date(`add_time`);
查询近30天的信息记录:
1 select * from `article` where date_sub(curdate(), INTERVAL 30 DAY) <= date(`add_time`);
查询本月的信息记录:
1 select * from `article` where date_format(`add_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m');
查询上一月的信息记录:
1 select * from `article` where period_diff(date_format(now() , ‘%Y%m') , date_format(`add_time`, ‘%Y%m')) =1;
对上面的SQL语句中的几个函数做一下分析: