MySQL counts the number of records per hour by time

Source: Internet
Author: User

MySQL time-per-hour record number scheme 1:?
1234567 SELECT@rownum := @rownum + 1 AS ID,        CONCAT((CASE WHEN t.hour = 24 THEN 0 ELSE t.hour END),‘:00:00‘) AS TIME, COUNT(*) AS COUNTFROM (SELECT id, HOUR(DATE_FORMAT(createtime,‘%H:%i:%s‘))+1 AS HOUR FROM baby WHERE DATE_FORMAT(createtime,‘%Y-%m-%d‘)=‘2016-10-16‘) t,     (SELECT @rownum := 0) rGROUP BY TIME ORDER BY ID//这个计数, 是LESS THAN TIME的计数方法, 即11:xx:xx的记录是记到12:00:00下面的, 而不是11:00:00;
The above modification?
12345 SELECT@rownum := @rownum + 1 AS ID,        CONCAT((CASE WHEN t.hour = 24 THEN 0 ELSE t.hour END),‘‘) AS TIME, COUNT(*) AS COUNTFROM (SELECT id, HOUR(DATE_FORMAT(createtime,‘%H:%i:%s‘)) AS HOUR FROM baby WHERE DATE_FORMAT(createtime,‘%Y-%m-%d‘)=‘2016-10-16‘) t,     (SELECT @rownum := 0) rGROUP BY TIME ORDER BY ID
Scenario 2?
1 SELECTLEFT( createtime, 13 ) , COUNT( * ) FROM baby WHERE DATE_FORMAT(createtime,‘%Y-%m-%d‘)=‘2016-10-16‘ GROUP BYLEFT( createtime, 13 )

If the current hour no data currently cannot return 0! I don't know how to handle it.

MySQL counts the number of records per hour by time

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.