mysql常用统计sql


1 、 查看当天日期

select current_date(); 

2、 查看当天时间

select current_time(); 

3、查看当天时间日期

select current_timestamp(); 

4、查询当天记录

select * from 表名 where to_days(时间字段名) = to_days(now()); 

5、查询昨天记录

select * from 表名 where to_days( now( ) ) – to_days( 时间字段名) <= 1 

6、查询7天的记录

select * from 表名 where date_sub(curdate(), interval 7 day) <= date(时间字段名)  

7、查询近30天的记录

select * from 表名 where date_sub(curdate(), interval 30 day) <= date(时间字段名) 

8、查询本月的记录

select * from 表名 where date_format( 时间字段名,%y%m’ ) = date_format( curdate( ) ,%y%m’ ) 

9、查询上一月的记录

select * from 表名 where period_diff( date_format( now( ) ,%y%m’ ) , date_format( 时间字段名,%y%m’ ) ) =1 

10、查询本季度数据

select * from 表名 where quarter(create_date)=quarter(now()); 

11、查询上季度数据

select * from 表名 where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter)); 

12、查询本年数据

select * from 表名  where year(create_date)=year(now()); 

13、查询上年数据

select * from 表名 where year(create_date)=year(date_sub(now(),interval 1 year)); 

14、查询当前这周的数据

select * from 表名 where yearweek(date_format(时间字段名,'%y-%m-%d')) = yearweek(now()); 

15、查询上周的数据

select * from 表名 where yearweek(date_format(时间字段名,'%y-%m-%d')) = yearweek(now())-1; 

16、查询当前月份的数据

select * from 表名   where date_format(时间字段名,'%y-%m')=date_format(now(),'%y-%m') 

17、查询距离当前现在6个月的数据

select * from 表名 where 时间字段名 between date_sub(now(),interval 6 month) and now(); 

18、查询距离当前现在12个月的数据

select * from 表名 t where DATE_FORMAT(时间字段,'%Y-%m')> DATE_FORMAT(date_sub(curdate(), interval 12 month),'%Y-%m');

文章作者: wmg
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 wmg !
  目录