MySQL 統計前一天日誌 本周日誌 某一天日誌

來源:互聯網
上載者:User

在mysql資料庫中,統計當天的日誌:

例如,統計bean資料庫 日期欄位為:usedate

統計當天的日誌sql語句:

select * from bean where date(usedate) = curdate();

curdate()表示當天日期

如果表示前一天的資料,則不能使用curdate()-1,因為當日期為月初時,curdate()-1 日期就不是上一個月的月末日期

例如:今天是7月1日,理論上curdate()-1為6月30日,但是curdate()-1得到不是6月30日。

那麼統計前一天的日期就不能使用curdate()-1了,mysql資料庫又有一個新方法統計前一天的資料

統計前一天的日誌sql語句:

select * from bean where date(usedate) = date_sub(curdate(),interval 1 day);

括弧中為當天時間的前一天,如果統計前幾天就將括弧中的’1’改成相應的天數。

統計本周日誌

要求: 統計從昨天開始統計前7天的日誌包括昨天

例如:今天7月1日,統計2010-6-24 ———— 2010-6-30 之間的資料

select * from bean where date(usedate) >= date_sub(curdate(),interval 7 day) 
and date(usedate) <=  date_sub(curdate(),interval 1 day)
 
在網上找的使用week統計一周資訊,只能統計到5天的資訊,不符合要求,所以改用這種方法。

統計曆史某一天的日誌

將date_sub(curdate(),interval 1 day)函數中的curdate()替換為某一天的日期

比如:要統計2010-07-05日期的資訊

date_sub('2010-07-05',interval 1 day)

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.