MySQL查询 本周/上周/本月/上个月份的数据

相关的sql代码如下:(亲测有效,其中created_time为表table中的时间字段)
查询本周数据:

SELECT   *  FROM  table
WHERE   YEARWEEK(date_format(created_time,'%Y-%m-%d')) = YEARWEEK(now())

查询上周数据:

 SELECT   *  FROM  table
  WHERE   YEARWEEK(date_format(created_time,'%Y-%m-%d')) = YEARWEEK(now())-1

查询本月数据:

 SELECT   *  FROM  table
 WHERE       date_format(created_time, '%Y-%m' ) = date_format( now( ), '%Y-%m' )

查询上月数据:

SELECT   *  FROM  table
 WHERE      period_diff(date_format(now() , '%Y%m') , date_format(`created_time`, '%Y%m')) =1

查询某月数据:

select * from table where date_format(created_time,'%Y-%m')='2019-05'

你可能感兴趣的:(SQL,java开发)