mysql如何分別按年/月/日/周分組統(tǒng)計數(shù)據(jù)詳解
1.統(tǒng)計
我們可以使用date_format()函數(shù)格式化時間,然后進行分組操作
例如有一個學生表,結構如下
| 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函數(shù),可以使用對應的year()/month()/week()/day()函數(shù)替代
2.占位符
date_format()需要傳入一個特定的占位符,mysql常用的占位符可參考下表
| 占位符 | 說明 |
|---|---|
| %Y | 年(4位) |
| %y | 年(2位) |
| %M | 月(英文名,如January) |
| %m | 月(數(shù)字,如01) |
| %D | 日(英文名,如1st) |
| %d | 日(數(shù)字,如01) |
| %e | 日(數(shù)字,如1) |
| %U | 一年中的第幾周,從0開始 ,周日是第一天 |
| %u | 一年中的第幾周,從0開始,周一是第一天 |
| %H | 時,24小時制,例如15 |
| %h | 時,12小時制,例如01 |
| %i | 分 |
| %s | 秒 |
補充:Mysql如何指定日期按周分組,并按次數(shù)分類統(tǒng)計
需要統(tǒng)計今年以來,每周用戶的提問次數(shù),并按提問次數(shù)進行分類統(tǒng)計,格式如下:

需求不能按自然周算,所以不適合用week()函數(shù)計算周數(shù),只能根據(jù)提問時間和指定日期的時間差相除
第幾周 =floor((提問時間-指定日期)/7)+1
確定了計算方式,首先按周和用戶ID分組,查詢周,提問次數(shù),然后通過case when對查詢結果按提問次數(shù)分類,sql如下:
SELECT weeks,
CONCAT(LEFT(MIN(create_time),10),"至",LEFT(MAX(create_time),10)) AS date_range,
SUM(CASE WHEN nums <= 5 THEN 1 ELSE 0 END) AS '提問次數(shù)<=5',
SUM(CASE WHEN nums > 5 AND nums<=10 THEN 1 ELSE 0 END) AS '5<提問次數(shù)<=10',
SUM(CASE WHEN nums > 10 THEN 1 ELSE 0 END) AS '提問次數(shù)>10'
FROM
(SELECT FLOOR(DATEDIFF(create_time,'2021-01-01')/7)+1 weeks,
create_time,uid,COUNT(*) AS nums
FROM `youTable`
WHERE create_time > "2021-01-01"
GROUP BY weeks,uid ORDER BY create_time ASC LIMIT 100000)
AS topic_table GROUP BY weeks

總結
到此這篇關于mysql如何分別按年/月/日/周分組統(tǒng)計數(shù)據(jù)的文章就介紹到這了,更多相關mysql分組統(tǒng)計數(shù)據(jù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mysql數(shù)據(jù)庫表增添字段,刪除字段,修改字段的排列等操作
這篇文章主要介紹了mysql數(shù)據(jù)庫表增添字段,刪除字段,修改字段的排列等操作,修改表指的是修改數(shù)據(jù)庫之后中已經(jīng)存在的數(shù)據(jù)表的結構2022-07-07
MySQL-group-replication 配置步驟(推薦)
下面小編就為大家?guī)硪黄狹ySQL-group-replication 配置步驟(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
MYSQL根據(jù)分組獲取組內多條數(shù)據(jù)中符合條件的一條(實例詳解)
這篇文章主要介紹了MYSQL根據(jù)分組獲取組內多條數(shù)據(jù)中符合條件的一條,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
MySQL遠程連接丟失問題解決方法(Lost connection to MySQL server)
這篇文章主要介紹了MySQL遠程連接丟失問題解決方法,Mysql錯誤Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0解決方法,需要的朋友可以參考下2014-06-06

