当前位置:网站首页>【mysql】mysql分别按年/月/日/周分组统计数据
【mysql】mysql分别按年/月/日/周分组统计数据
2022-08-10 23:50:00 【冰冷的希望】
1.统计
我们可以使用date_format()函数格式化时间,然后进行分组操作
例如有一个学生表,结构如下
id | name | age | height | gender | create_time |
---|---|---|---|---|---|
1 | pan | 27 | 169 | 1 | 2022-01-13 10:20:22 |
2 | yang | 18 | 177 | 1 | 2022-03-14 09:16:42 |
3 | daisy | 25 | 156 | 2 | 2022-07-19 19:58:03 |
-- 按年
select date_format(create_time, '%Y') years,avg(age),count(gender) from student where create_time > "2022-01-01 00:00:00" and create_time < "2022-07-19 19:58:03" group by years;
-- 按月
select date_format(create_time, '%Y-%m') months,avg(age),count(gender) from student where create_time > "2022-01-01 00:00:00" and create_time < "2022-07-19 19:58:03" group by months;
-- 按周
select date_format(create_time, '%Y-%u') weeks,avg(age),count(gender) from student where create_time > "2022-01-01 00:00:00" and create_time < "2022-07-19 19:58:03" group by weeks;
-- 按日
select date_format(create_time, '%Y-%m-%d') days,avg(age),count(gender) from student where create_time > "2022-01-01 00:00:00" and create_time < "2022-07-19 19:58:03" group by days;
如果不想用date_format函数,可以使用对应的year()/month()/week()/day()函数替代
2.占位符
date_format()需要传入一个特定的占位符,mysql常用的占位符可参考下表
占位符 | 说明 |
---|---|
%Y | 年(4位) |
%y | 年(2位) |
%M | 月(英文名,如January) |
%m | 月(数字,如01) |
%D | 日(英文名,如1st) |
%d | 日(数字,如01) |
%e | 日(数字,如1) |
%U | 一年中的第几周,从0开始 ,周日是第一天 |
%u | 一年中的第几周,从0开始,周一是第一天 |
%H | 时,24小时制,例如15 |
%h | 时,12小时制,例如01 |
%i | 分 |
%s | 秒 |
边栏推荐
- [Excel knowledge and skills] Convert numeric format numbers to text format
- 【pypdf2】安装、读取和保存、访问页面、获取文本、读写元数据、加密解密
- 鲲鹏编译调试及原生开发工具基础知识
- Lens filter---about day and night dual-pass filter
- 12. Handling JSON
- 工程师如何对待开源
- CF1427F-Boring Card Game【贪心】
- 编程语言为什么有变量类型这个概念?
- YOLOv5的Tricks | 【Trick12】YOLOv5使用的数据增强方法汇总
- Timers, synchronous and asynchronous APIs, file system modules, file streams
猜你喜欢
YOLOv5的Tricks | 【Trick12】YOLOv5使用的数据增强方法汇总
iNFTnews | In the Web3 era, users will have data autonomy
Excel English automatic translation into Chinese tutorial
[Excel知识技能] 将文本型数字转换为数值格式
Jvm.分析工具(jconsole,jvisualvm,arthas,jprofiler,mat)
基于SSM实现手机销售商城系统
7. yaml
学习Apache ShardingSphere解析器源码(一)
ROS Experimental Notes - Install QPEP and Intel-MKL
SQL注入基础
随机推荐
iNFTnews | Web3时代,用户将拥有数据自主权
【C语言】C语言程序设计:动态通讯录(顺序表实现)
HGAME 2022 Final Pokemon v2 writeup
逮到一个阿里 10 年老 测试开发,聊过之后收益良多...
App regression testing, what are the efficient testing methods?
开启新征程——枫叶先生第一篇博客
ROS实验笔记之——UZH-FPV数据集的验证
Activiti7子流程之Call activity
Is there a way out in the testing industry if it is purely business testing?
In 22 years, the salary of programmers nationwide in January was released, only to know that there are so many with annual salary of more than 400,000?
宝塔实测-搭建PHP在线模拟考试系统
How to determine how many bases a number is?
[C] the C language program design, dynamic address book (order)
Timers, synchronous and asynchronous APIs, file system modules, file streams
SAS data processing technology (1)
How to recover deleted files from the recycle bin, two methods of recovering files from the recycle bin
Multilingual Translation - Multilingual Translation Software Free
基于Web的疫情隔离区订餐系统
ROS Experimental Notes - Install QPEP and Intel-MKL
iNFTnews | In the Web3 era, users will have data autonomy