按月统计
统计数据表[TABLE_NAME]里边,[TIMESTAMP_COLUMN_NAME]这个字段按月的数据量
>> select [TIMESTAMP_COLUMN_NAME], DATE_FORMAT([TIMESTAMP_COLUMN_NAME],'%Y-%m') as month, count(*) as sum from [TABLE_NAME] group by month;
按天统计
统计数据表[TABLE_NAME]里边,[TIMESTAMP_COLUMN_NAME]这个字段按天的数据量
>> select [TIMESTAMP_COLUMN_NAME], DATE_FORMAT([TIMESTAMP_COLUMN_NAME],'%Y-%m-%d') as day, count(*) as sum from [TABLE_NAME] group by day;
按天统计和按月统计的区别就是多了一个“-%d"
统计7天内的数据
统计数据表[TABLE_NAME]里边,[TIMESTAMP_COLUMN_NAME]这个字段到今天包含今天的7天内的数据量
>> select DATE_FORMAT([TIMESTAMP_COLUMN_NAME],'%Y-%m-%d') as date, count(1) as sum from [TABLE_NAME] where [TIMESTAMP_COLUMN_NAME] > date(now()) - interval 6 day group by day([TIMESTAMP_COLUMN_NAME]);
统计时间差
计算从2020年8月21日到现在的天数
>> select to_days(now()) - to_days('20200821') as datediff;
计算数据表[TABLE_NAME]里边,[TIMESTAMP_COLUMN_NAME]这个字段从有数据到现在的天数和总的记录数
>> select to_days(now()) - to_days(min([TIMESTAMP_COLUMN_NAME])) as days, count(*) from [TABLE_NAME];
如果语句有什么错误的地方,欢迎纠正,多谢~