MySQL按時間統計資料的方法總結

2022-09-25 09:21:08 字數 3298 閱讀 5857

在做資料庫的統計時,經常會需要根據年、月、日來統計資料,然後配合echarts來製作視覺化效果。

資料庫:mysql

思路按照時間維度進行統計的前提是需要資料庫中有保留時間資訊,建議是使用mysql自帶的datetime型別來記錄時間。

`timestamp程式設計客棧` datetime default null,

在mysql中對於時間日期的處理的函式主要是date_format(date,format)。可用的引數如下

格式描述

%a縮寫星期名

%b縮寫月名

%c月,數值

%d帶有英文本首的月中的天

%d月的天,數值(00-31)

%e月的天,數值(0-31)

%f微秒

%h小時 (00-23)

%h小時 (01-12)

%i小時 (01-12)

%i分鐘,數值(00-59)

%j年的天 (001-366)

%k小時 (0-23)

%l小時 (1-12)

%m月名

%m月,數值(00-12)

%pam 或 pm

%r時間,12-小時(hh:mm:ss am 或 pm)

%s秒(00-59)

%s秒(00-59)

%t時間, 24-小時 (hh:mm:ss)

%u周 (00-53) 星期日是一周的第一天

%u周 (00-53) 星期一是一周的第一天

%v周 (01-53) 星期日是一周的第一天,與 %x 使用

%v周 (01-53) 星期一是一周的第一天,與 %x 使用

%w星期名

%w周的天 (0=星期日, 6=星期六)

%x年,其中的星期日是周的第一天,4 位,與 %v 使用

%x年,其中的星期一是周的第一天,4 位,與 %v 使用

%y年,4 位

%y年,2 位

注:當涉及到按日統計是,需要使用%j,而如果使用%d, %e, %w的話,那麼不同月份/週裡的相同值會統計在一起。

涉及到獲取當前時間,則可以通過now()或者sysdate()來獲取。

select sysdate() from dual;

select now() from dual;

按照實際需求使用group by查詢即可。

結論需統計的表結構如下:

create table `apilostgzdimog` (

`id` int(11) not null auto_increment,

`username` varchar(64) default null,

`action` varchar(64) default null,

`params` text,

`result` text,

`timestamp` datetime default null,

primary key (`id`)

)統計時間範圍內不同分類action的數量

# 當日

select action, count(id) count from apilog where date_format(`timestamp`,'%j') = date_format(now(),'%j') order by count desc;

# 當周

select action, count(id) count from apilog where date_format(`timestamp`,'%u') = date_format(now(),'%u') order by count desc;

# 當月

select action, count(id) count from apilog where date_format(`timestamp`,'%m') = date_format(now(),'%m') order by count desc;

# 當年

select action, count(id) count from apilog where date_format(`timestamp`,'%y') = date_format(now(),'%y') order by count desc;

統計某分類action的時間維度數量

# 按日

select action, date_format(`timestamp`,'%j'), count(id) count from apilog where action = '***' group by date_format(`timestamp`,'%j')

# 按周

select action, date_format(`timestamp`,'%u'), count(id) count from apilog where action = '***' group by date_format(`timestamp`,'%u')

# 按月

select action, date_format(`timestamp`,'%m'), count(id) count from apilog where action = '***' group by date_format(`timestamp`,'%m')

# 按年

se程式設計客棧lect action, date_format(`timestamp`,'%y'), count(id) count from apilog where action = '***' group by date_format(`timestamp`,'%y')

同時按action和時間維度統計

# 按日

select action, date_format(`timestamp`,'%j'), count(id) count from apilog group by action, date_format(`timestamp`,'%j')

# 按周

select action, date_format(`timestamp`,'%u'), count(id) co程式設計客棧unt from apilog group by action, date_format(`timestamp`,'%u')

# 按月

select action, date_format(`timestamp`,'%m'), count(id) count from apilog group by action, date_format(`timestamp`,'%m')

# 按年

select action, date_format(`timestamp`,'%y'), count(id) count from apilog group by action, date_format(`timestamp`,'%y')

mysql根據時間統計資料 建立時間臨時輔助表

我們平時開發中偶爾會遇到統計,但是有時候根據時間統計的時候,有些時間內資料為空,這時候我們前端的開發人員不知道具體是那一天 或者別的時間點 這個時候我們需要一張時間的輔助表 通過和時間輔助表聯查獲得那一天資料為空的時間 1 建表 drop table if exists t calendar aux...

mysql按周統計資料

主要就是使用date format這個方法 select date format createtime,y u weeks,count count from user group by weeks order by weeks desc u 周 00 53 星期日是一周的第一天 u 周 00 53 ...

MySQL列轉行統計資料

筆者在開發的過程中遇到了這樣的問題。其中item id是固定的字典。值是死的。其中item id 等於 1,2,3的值是每乙個work quoted id都需要相乘的。相當於乙份訂單人工費和施工人數以及施工天數都是一定存在的並且是相乘的關係。其他的item id的值是需要相加的。所以,運算的規則是 ...