变量以及函数说明:

add_time为插入的时间
to_days是sql函数,返回的是个天数
data_sub(date,INTERVAL expr type)给指定的日期减去多少天
data()函数返回日期或日期/时间表达式的日期部分。
curdate()函数返回当前的日期 y-m-d
data_format 用于以不同的格式显示日期/时间数据
period_diff(p1,p2)返回周期P1和P2之间的月数。 P1和P2格式为YYMM或YYYYMM。注意周期参数 P1 和 P2 都不是日期值
 
1、查询今天的所有记录:
    (1)add_time字段,该字段为int(5)类型的  
    select * from `article` where to_days(date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d')) = to_days(now());
    (2)add_time字段是DATETIME类型或者TIMESTAMP类型的    
    select * from `article` where to_days(`add_time`) = to_days(now());
2、查询昨天的所有记录
    
    select * from `article` where to_days(now()) = 1 + to_days(`add_time`);
3、近7天的信息记录:   
    select * from `article` where date_sub(curdate(), INTERVAL 7 DAY) <= date(`add_time`);
4、近30天的信息记录: 
    select * from `article` where date_sub(curdate(), INTERVAL 30 DAY) <= date(`add_time`);
5、查询本月的记录   
    select * from `article` where date_format(`add_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m');
6、上一个月的记录
    
    select * from `article` where period_diff(date_format(now() , ‘%Y%m') , date_format(`add_time`, ‘%Y%m')) =1;

相关文章:

  • 2021-08-15
  • 2021-10-11
  • 2022-12-23
  • 2022-01-29
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2021-09-26
猜你喜欢
  • 2022-02-08
  • 2021-05-27
  • 2022-12-23
相关资源
相似解决方案