MySQL collects logs of the previous day. logs of this week are collected on a specific day.

Source: Internet
Author: User

In the mysql database, count the logs of the current day:

For example, the Date Field of the bean database is usedate.

Calculate the log SQL statement for the current day:

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

Curdate () indicates the date of the current day.

If it indicates the data of the previous day, you cannot use curdate ()-1, because when the date is the beginning of the month, curdate ()-1 is not the last date of the previous month

For example, Today is July 22, July 1. In theory, curdate ()-1 is July 22, June 30, but curdate ()-1 is not July 22, June 30.

Therefore, you cannot use curdate ()-1 to calculate the date of the previous day, and the mysql database has a new method to calculate the data of the previous day.

Calculate the log SQL statement of the previous day:

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

It is the day before the current day in parentheses. If the number of days is counted, change '1' in brackets to the corresponding number of days.

Weekly log statistics

Requirement: collect statistics from yesterday. the first seven days of logs include yesterday.

For example, we will collect data from July 1.

select * from bean where date(usedate) >= date_sub(curdate(),interval 7 day) 
and date(usedate) <=  date_sub(curdate(),interval 1 day)
 
Week is used on the Internet to collect weekly information, which can only be used for 5 days. This method is used because it does not meet the requirements.

Collect logs of a previous day

Replace curdate () in the date_sub (curdate (), interval 1 day) function with the date of a certain day.

For example, you need to collect the information of the date.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.