当前位置:网站首页>[MySQL] MySQL groups statistical data by year / month / day / week
[MySQL] MySQL groups statistical data by year / month / day / week
2022-07-17 22:54:00 【Cold hope】
1. Statistics
We can use date_format() Function formatting time , Then group
For example, there is a student list , The structure is as follows
| 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 |
-- By year
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;
-- monthly
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;
-- By week
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;
-- By day
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;
If you don't want to use date_format function , You can use the corresponding year()/month()/week()/day() Function substitution
2. Place holder
date_format() You need to pass in a specific placeholder ,mysql Common placeholders can refer to the following table
| Place holder | explain |
|---|---|
| %Y | year (4 position ) |
| %y | year (2 position ) |
| %M | month ( English name , Such as January) |
| %m | month ( Numbers , Such as 01) |
| %D | Japan ( English name , Such as 1st) |
| %d | Japan ( Numbers , Such as 01) |
| %e | Japan ( Numbers , Such as 1) |
| %U | Weeks of the year , from 0 Start , Sunday is the first day |
| %u | Weeks of the year , from 0 Start , Monday is the first day |
| %H | when ,24 hourly , for example 15 |
| %h | when ,12 hourly , for example 01 |
| %i | branch |
| %s | second |
边栏推荐
- C language basics: binary search
- STM32F407外部SRAM
- [untitled]
- MySQL——mysql下载地址
- 图解数组计算模块Numpy上(指定数值类型、数值类型、二维数组索引、二维数组切片索引、数组重塑、数组的增、删、改、查、矩阵运算(mat)、矩阵求逆)
- Sword finger offer interview question: 2 Search in two-dimensional array
- 如何启动MySQL?
- 海思万能平台搭建:在线调试3VsCode插件
- How to solve common errors of JMeter
- Scratch draw changeable squares Electronic Society graphical programming scratch grade examination true questions and answers analysis June 2022
猜你喜欢

Understanding and operation of pointer

ArkUI常见问题汇总【系列3】

Reinstallation method of U disk startup disk of win10 system

26 Clickhouse usage, mergetree read / write process and six common table engines

【重识云原生】第四章云网络4.9.4.1节——智能网卡SmartNIC方案综述

Wx applet learning notes day02

Scratch draw changeable squares Electronic Society graphical programming scratch grade examination true questions and answers analysis June 2022

How to solve common errors of JMeter

c语言基础篇:操作符

List集合
随机推荐
Learning notes of inclusion exclusion principle
Std:: vector to array to eigen:: matrix
Talk about digital transformation Office
c语言基础篇:猜数字小游戏
Live broadcast preview | build sophisticated e-commerce operations to help retail Omni channel growth
容斥原理学习笔记
海思万能平台搭建:在线调试2ssh的移植部署
【mysql】mysql分别按年/月/日/周分组统计数据
如何启动MySQL?
IO exception handling
Postman接口测试工具
Quelle est la relation entre l'espace de table d'Oracle et la base de données?
Sword finger offer interview question: 2 Search in two-dimensional array
The first article of participating in the publishing activity
VBA drives SAP GUI to complete initialization of interface element value
Set集合
数组的理解与操作
List集合
c语言基础篇:操作符
Schéma de traitement à haut taux d'utilisation des Redo