MySQL中按天 自然周 月 季度 年份統計

2021-09-10 07:47:00 字數 1657 閱讀 4025

在oracle資料庫中,通過to_char()函式來操作日期變數,而在mysql中,則通過date_format()函式實現日期相關週期的統計。date_format()函式一共有兩個引數:date引數表示日期變數,format引數表示日期格式。

如果想檢視date_format()函式的具體引數及其取值情況,可以搜尋引擎中自行查詢。比如輸入「mysql date_format」可以看到w3school中對該函式的詳細介紹。

select date_format(postdatetime,'%y年%m月%d日') as d,count(*)

from table

group by date_format(postdatetime,'%y%m%d')

order by d asc;

select date_format(postdatetime,'%y年%u周') as w,min(postdatetime) as st,count(*)

from table

group by date_format(postdatetime,'%y%u')

order by w asc;

輸出結果如下所示:

select date_format(postdatetime,'%y年%m月') as m,count(*) 

from table

group by date_format(postdatetime,'%y%m')

order by m asc

select floor((date_format(postdatetime,'%m')-1)/3)+1 as q,min(postdatetime) as st,count(*)

from table

where date_format(postdatetime,'%y') = 2018

group by floor((date_format(postdatetime,'%m')-1)/3)+1

order by q asc;

使用到了floor函式,根據月份去判斷所屬季節,並輸出季節對應的起始月份。

結果如下所示:

???再看看我們以前的文章???

? excel中資料分析工具庫-相關係數篇

? 乾貨,手把手教會你做相關性分析

? 5年資料分析路,小結。

? 使用者細分及畫像分析

? k-近鄰演算法及實踐

C 自然周,月,季度計算。

判斷時間是否和伺服器時間是一天 public static bool judgetimeistoday datetime cs 計算當前季度多少天 public static intdatediff 計算當前月有多少天 public static intgetmonthdays 計算本週的周一日期 ...

Mysql 查詢天 周,月,季度 年的資料

今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1近7天 select from 表名 where date sub curdate interva...

Mysql 常用的時間查詢(周,月,季,年)

一 查詢當日資料 1 select from v2 goods base price as v2 where date v2.created at curdate 2 select from v2 goods base price as v2 where year v2.created at yea...