MySql calculates every day, every hour, every minute, and every minute in a period of time.

Source: Internet
Author: User

MySql calculates every day, every hour, every minute, and every minute in a period of time.

Statistics on each day in a period of time or at each time point are usually calculated. However, mysql itself does not directly obtain the time point list function or table. The following are some of the methods used by you to associate a temporary variable with an existing table that has more data (depending on the actual situation) to obtain the time point list. Hope to help you.

Some key data used in SQL

# T_table is an existing table with more data on the system.

Every day of a month

# SET @ beginDate = '2017-07-01 '; SET @ maxDate = '2017-07-31'; select date (@ tempDay), @ tempDay: = DATE_ADD (@ tempDay, INTERVAL 1 DAY) FROM t_table f # This table is an existing table with more data in the system, left join (SELECT @ tempDay: = @ beginDate) B ON 1 = 1 WHERE @ tempDay <= @ maxDate

 

Every hour of yesterday

# SET @ beginDate = DATE (DATE_ADD (NOW (), INTERVAL-1 DAY); SET @ maxDate = DATE (NOW ()); SELECT DATE_FORMAT (@ tempHour, '% H'), @ tempHour: = DATE_ADD (@ tempHour, INTERVAL 1 HOUR) FROM t_table fLEFT JOIN (SELECT @ tempHour: = @ beginDate) B ON 1 = 1 WHERE @ tempHour <@ maxDate

 

Every minute in an hour

# SET @ beginDate = '2017-08-01 17:00:00 'every minute in an hour; SET @ maxDate = '2017-08-01 18:00:00'; SELECT DATE_FORMAT (@ tempMinute, '% I'), @ tempMinute: = DATE_ADD (@ tempMinute, INTERVAL 1 MINUTE) FROM t_table tLEFT JOIN (SELECT @ tempMinute: = @ beginDate) B ON 1 = 1 WHERE @ tempMinute <@ maxDate

 

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.