獲取當前時間
CURTIME();
查詢今天的數據
select * from table where to_days(time) = to_days(now())
查詢昨天數據
select * from error where to_days(now())-to_days(alarmtime)=1
查詢最近一個星期數據
select * from error where to_days(now())-to_days(alarmtime)<=7
SELECT * FROM error where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(alarmtime)
查詢某天后一個星期的數據
SELECT * FROM error where DATE_ADD('2018-12-13', INTERVAL 7 DAY)
以上查詢語句若有一天沒有數據就沒有那一天的顯示
要想每天都顯示 空的補0 可以用下面的sql
構建一個最近七天的結果集,然后和查詢的結果集合做left join
select a.click_date,ifnull(b.count1,0)as count1 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 date(alarmtime) as datetime, count(*) as count1 from error where type=1 group by date(alarmtime) ) b on a.click_date = b.datetime