mysql實現統計今日資料 近七日資料及近八周資料

2021-10-01 18:31:44 字數 3902 閱讀 4989

因為工作上的需求,需要統計本日資料、近七日資料及近八周資料,特此記錄一下,如果能幫到其他人倍感榮幸。

##本日資料

為了統計方便做了補零處理,即沒有資料就補零

select a.

hour

hour

, ifnull(b.count,

0) count from

(select

0hour

union

allselect

1hour

union

allselect

2hour

union

allselect

3hour

union

allselect

4hour

union

allselect

5hour

union

allselect

6hour

union

allselect

7hour

union

allselect

8hour

union

allselect

9hour

union

allselect

10hour

union

allselect

11hour

union

allselect

12hour

union

allselect

13hour

union

allselect

14hour

union

allselect

15hour

union

allselect

16hour

union

allselect

17hour

union

allselect

18hour

union

allselect

19hour

union

allselect

20hour

union

allselect

21hour

union

allselect

22hour

union

allselect

23hour

) a left

join

(select

date_format( t.create_time,

'%h')as

hour

,count(*

) count

from tbale t

where datediff(t.create_time,

now())

=0group

byhour

) b on a.

hour

=b.hour

order

byhour

##近七日資料

這個是近七日資料,不是本週資料,小夥伴注意區分,也做了補零處理

select a.click_date as

data

,ifnull(b.count,0)

as count

from

(select curdate(

)as click_date

union

allselect date_sub(curdate(),

interval

1day

)as click_date

union

allselect date_sub(curdate(),

interval

2day

)as click_date

union

allselect date_sub(curdate(),

interval

3day

)as click_date

union

allselect date_sub(curdate(),

interval

4day

)as click_date

union

allselect date_sub(curdate(),

interval

5day

)as click_date

union

allselect date_sub(curdate(),

interval

6day

)as click_date

) a left

join

(select

date

(t.create_time)

asdatetime

,count(*

)as count

from tbale t

group

bydate

(t.create_time)

) b on a.click_date = b.

datetime

order

bydata

##近八周資料

這部分需要重點注意一下,mysql中的week函式預設是以週日為沒周的第一次計算的,不符合國情,所以我做了處理改為周一了,大家直接用就行了

select tbl._date as datestring,ifnull(tbr.allfare,0)

as count

from

(select

(@s:=@s+

1)as _index,week(

date

(date_sub(

current_date

,interval

@s week)),

1)as _date

from information_schema.character_sets,

(select

@s:=-1

)as init where

@s<

7order

by _date)

as tbl

left

join

(select

count(1

) allfare,week(t.create_time,1)

as finish_date

from tbale t

where week(t.create_time,1)

>= week(

date

(date_sub(

current_date

,interval

7 week)),

1)and week(t.create_time,1)

<= week(

current_date,1

)group

by finish_date order

by finish_date

)as tbr on tbl._date = tbr.finish_date group

by tbl._date;

當然裡面是有坑的,有點mysql版本設定問題執行一些函式會報錯,別慌,

輸入下面這段sql就可以了

set @@global.sql_mode

='strict_trans_tables,no_zero_in_date,no_zero_date,error_for_division_by_zero,no_auto_create_user,no_engine_substitution'

希望可以幫到大家,謝謝

mysql統計近七天的資料並分組沒有的為0

select a.click date,ifnull b.count,0 count from select curdate as click date union all select date sub curdate interval 1 day as click date union all ...

Mysql統計昨日今日本月本週資料

一 sql示例 1 統計本週資料 select count from system log where week from unixtime update time week now 2 統計本月資料 select count from system log where monthname from...

mysql 統計七天資料並分組

統計各機型最近7天bug數量來支撐一張圖表 sql需要查詢最近七天資料並按每天和機型進行分組 select from table where date sub curdate interval 7 day date column time 拓展 查詢最近一天的資料 select from table...