1. 程式人生 > >sql刪除一個月前的所有資料

sql刪除一個月前的所有資料

delete from recycle where time < date_add(curdate(),INTERVAL -1 month)


其中我的time欄位是datetime型別


一天 select * from T_news where datediff(day,addtime,getdate())=0


三天 select * from T_news where datediff(day,addtime,getdate())<= 2 and datediff(day,addtime,getdate())>= 0


一週 select * from T_news WHERE (DATEPART(wk, addtime) = DATEPART(wk, GETDATE())) AND (DATEPART(yy, addtime) = DATEPART(yy, GETDATE()))


注意:此時不能用 datediff 差值為7,因為,datediff只表示間隔數


一月    select * from T_news WHERE (DATEPART(yy, addtime) = DATEPART(yy, GETDATE())) AND (DATEPART(mm, addtime) = DATEPART(mm, GETDATE()))


一季度 select * from T_news where DATEPART(qq, addtime) = DATEPART(qq, GETDATE()) and DATEPART(yy, addtime) = DATEPART(yy, GETDATE())