MySQL按日期分組統計資料

2021-10-10 18:55:09 字數 1027 閱讀 8480

mysql按日期分組統計資料

// 查詢今天的資料

select * from 表名 where to_days(時間字段) = to_days(now());
// 查詢昨天的資料

select * from 表名 where to_days(now()) - to_days(時間字段) <= 1;
// 查詢最近七天的資料

select * from 表名 where date_sub(curdate(), interval 7 day) <= date(時間字段);
按月統計(一)

select date_format(create_time, '%y-%m') mont, count(*) coun

from t_content

group by date_format(create_time, '%y-%m');

按天統計(二)

select date_format(create_time, '%y-%m-%d') dat, count(*) coun

from t_content

group by date_format(create_time, '%y-%m-%d');

按天統計(三)

select from_unixtime(create_time / 1000, '%y-%m-%d') dat, count(*) coun

from t_content

group by from_unixtime(create_time / 1000, '%y-%m-%d')

其他

格式轉換

select from_unixtime(create_time / 1000, '%y-%m-%d %h:%i:%s') create_time

from t_content

常用日期分組統計資料

在日常統計資料中 經常有用到按照日期或者周或者月或者年來作為統計分組 主要用的函式date format 常用如 1 select date format create time,y u weeks,count caseid count from tc case group by weeks 2 s...

MySql按周,按月,按日分組統計資料

select date format create time,y u weeks,count caseid count from tc case group byweeks select date format create time,y m d days,count caseid count fr...

MySql按周,按月,按日分組統計資料

select date format create time,y u weeks,count caseid count from tc case group by weeks select date format create time,y m d days,count caseid count f...