MySQL Time period statistics problem

Source: Internet
Author: User
My database is MySQL the Time field type is timestamp
This database inserts a record every once in a while, similar to the following:

id  |       datetime       |  data01  |  data021   | 2015-06-10 00:00:00  |   23.8   |   33.72   | 2015-06-10 00:01:11  |   21.8   |   12.73   | 2015-06-10 00:01:30  |   21.8   |   33.74   | 2015-06-10 01:02:00  |   23.8   |   33.75   | 2015-06-10 01:03:10  |   23.8   |   33.7......209 | 2015-06-10 23:03:00  |   23.8   |   33.7

I now want to divide 24 hours statistics, such as the sum of all the data of 00:00:00-01:00:00, and so on, there is can only control the time, do not put the month and day also counted in, as long as the time interval,
Thank you all!

Reply content:

My database is MySQL the Time field type is timestamp
This database inserts a record every once in a while, similar to the following:

id  |       datetime       |  data01  |  data021   | 2015-06-10 00:00:00  |   23.8   |   33.72   | 2015-06-10 00:01:11  |   21.8   |   12.73   | 2015-06-10 00:01:30  |   21.8   |   33.74   | 2015-06-10 01:02:00  |   23.8   |   33.75   | 2015-06-10 01:03:10  |   23.8   |   33.7......209 | 2015-06-10 23:03:00  |   23.8   |   33.7

I now want to divide 24 hours statistics, such as the sum of all the data of 00:00:00-01:00:00, and so on, there is can only control the time, do not put the month and day also counted in, as long as the time interval,
Thank you all!

sqlselect count (*) from tablename where datetime between '2015-06-10 00:00:00' and '2015-06-10 01:00:00';

It's simple.

sqlselect substring(`datetime`,12,2) as `hour`,count(*) as `total` from `table` group by `hour`;
  • 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.