查詢最近七天的資料統計

2021-08-18 17:16:29 字數 1453 閱讀 6472

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 5 then '星期四' when 6 then '星期五' when 7 then '星期六' end week

from rm_ferrydata_log

where datediff(now(),operatetime)<=6

group by time

order by time;

這樣查詢,如果這週的資料沒記錄,ze無資料查出,『

優化: null 轉0, 沒有資料,就顯示時間,且數量設為0

select ifnull(b.count,0) count, a.click_date time,

case dayofweek(click_date) when 1 then '星期日' when 2 then '星期一' when 3 then '星期二' when 4 then '星期三' when 5 then '星期四' when 6 then '星期五' when 7 then '星期六' end week

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

) a

left join (

select count(0) count, date(operatetime) time

from rm_ferrydata_log

where datediff(now(),operatetime)<=6

group by time

) bon a.click_date=b.time

當天 三天 七天資料統計

語句一 with s as select cast getdate as date as shift date 1 as do code 當天入庫 as type union all select cast getdate as date as shift date 0 as do code 當天出...

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

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

sql語句查詢最近七天 三十天 資料

幾個小時內的資料 date sub now interval 5 hour 今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1 7天 select ...