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

2021-10-06 18:56:49 字數 2538 閱讀 1197

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

select

date_sub( curdate( ), interval 2 day ) as click_date union all

select

date_sub( curdate( ), interval 3 day ) as click_date union all

select

date_sub( curdate( ), interval 4 day ) as click_date union all

select

date_sub( curdate( ), interval 5 day ) as click_date union all

select

date_sub( curdate( ), interval 6 day ) as click_date 

) aleft join ( select date( create_date ) as create_date, count( * ) as count from vip_experience group by date( create_date ) ) b on a.click_date = b.create_date;

下面這個是我查詢的sql

select

a.*,

ifnull( b.inlet_flow, 0 ) inlet_flow,

ifnull( b.passen_flow, 0 ) passen_flow,

ifnull( b.entry_rate, 0 ) entry_rate

from

(select

curdate( ) as date union all

select

date_sub( curdate( ), interval 1 day ) as date union all

select

date_sub( curdate( ), interval 2 day ) as date union all

select

date_sub( curdate( ), interval 3 day ) as date union all

select

date_sub( curdate( ), interval 4 day ) as date union all

select

date_sub( curdate( ), interval 5 day ) as date union all

select

date_sub( curdate( ), interval 6 day ) as date 

) aleft join (

select

temp.date,

temp.inlet_flow,

temp.passen_flow,

temp.entry_rate 

from

(select

date_format( mt.trace_date, '%y-%m-%d' ) as date,

count( ve.create_date ) as inlet_flow,

count( mt.trace_date ) as passen_flow,

concat( round( count( ve.create_date ) / count( mt.trace_date ) * 100, 2 ), '', '%' ) as entry_rate 

from

member_footprint mt

left join vip_experience ve on mt.face_id = ve.face_id 

#and date_format( mt.trace_date, '%y-%m-%d' ) = date_format( ve.create_date, '%y-%m-%d' )

left join device d on mt.cid = d.dev_cid 

where

#date_sub( curdate( ), interval 7 day ) < date( now( ) ) 

#and

d.shop_id = 1 

group by

date_format( mt.trace_date, '%y-%m-%d' ) 

) temp 

) b on a.date = b.date 

order by

a.date asc

上面的select是做個7天的日期查詢,後面的sql是業務的資料,然後根據date就是格式化後的日期分組,然後用date就是具體的日期join一下,留下符合 日期條件的資料,可以join也可以用in做子查詢,看下下面的效果

mysql 統計七天資料並分組

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

統計最近七天的資料訪問量 mysql

最近做了乙個日誌功能,當使用者訪問介面後,會記錄哪個介面,返回結果等資訊,現在想做乙個統計最近七天的訪問介面數量,之前想的有些複雜。首先獲取今天的日期,根據日期查詢數量,再查詢出近七天的數量。後來發現sql可以直接解決,curdate 函式返回當前的日期。curdate 直接返回當天 查詢最近七天的...

查詢最近七天的資料統計

select count 0 count,date format operatetime,m d time,case dayofweek operatetime when 1 then 星期日 when 2 then 星期一 when 3 then 星期二 when 4 then 星期三 when ...