Detailed explanation of MySQL to obtain statistical data for each day and each hour of a certain period of time

Detailed explanation of MySQL to obtain statistical data for each day and each hour of a certain period of time

Get daily statistics

When doing a project, you need to analyze the project log. One of the requirements is to obtain the log data of each day in a given time period. For example, you need to obtain the log data of each day from 2018-02-02 09:18:36 to 2018-03-05 23:18:36. Generally, when you see this requirement, you consider using functions to solve it, and directly use SQL statements.

SELECT
	DATE_FORMAT(trigger_time, '%Y-%m-%d') triggerDay,
	COUNT(id) triggerCount
FROM
	`job_qrtz_trigger_log`
WHERE
	trigger_time BETWEEN '2018-02-02 09:18:36'
AND '2018-03-05 23:18:36'
GROUP BY triggerDay
ORDER BY trigger_time;

Query results:

A brief explanation of the above sql

First, query the field. Here, COUNT(id) triggerCount is the number of statistical data we need. You can add required fields according to actual needs.

DATE_FORMAT(trigger_time, '%Y-%m-%d') triggerDay

This is a date formatted into the format of YYYY-mm-dd. The format here is used for subsequent grouping, so you can customize the format according to different needs.

My requirement is to get the log data for each day, so I group them in the format of year-month-date and get them through count(*).

If you want to get the amount of data for each month in a certain month, change the format to

DATE_FORMAT(trigger_time, '%Y-%m')

Of course, the where condition needs to match the format. You cannot limit the where condition to the date and group by month.

The date output format of MySQL is listed below:

%a abbreviated day of the week
%b abbreviated month name
%c Month, numeric value
%D Day of the month with English prefix
%d Day of the month, numeric value (00-31)
%e Day of the month, numeric value (0-31)
%f microseconds
%H Hours (00-23)
%h Hour (01-12)
%I Hour (01-12)
%i Minute, value (00-59)
%j Day of the year (001-366)
%k hour (0-23)
%l hours (1-12)
%M Month name
%m Month, numeric value (00-12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss AM or PM)
%S Seconds (00-59)
%s seconds (00-59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00-53) Sunday is the first day of the week
%u Week (00-53) Monday is the first day of the week
%V Week (01-53) Sunday is the first day of the week, used with %X
%v Week (01-53) Monday is the first day of the week, used with %x
%W day of the week
%w day of the week (0=Sunday, 6=Saturday)
%X Year, where Sunday is the first day of the week, 4 digits, used with %V
%x Year, where Monday is the first day of the week, 4 digits, used with %v
%Y Year, 4 digits
%y Year, 2 digits

Get statistics for each hour of a day

Use the HOUR function that comes with Mysql to process

SELECT HOUR(trigger_time) as Hour,count(*) as Count 
  FROM xxl_job_qrtz_trigger_log 
  WHERE
	trigger_time BETWEEN '2018-02-05 01:18:36'
AND '2018-02-05 17:18:36'
  GROUP BY HOUR(trigger_time) ORDER BY Hour(trigger_time);

The query results are as follows

The above is the detailed explanation and integration of MySQL to obtain statistical data for every day and every hour in a certain period of time introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • MySQL statement to get all dates or months in a specified time period (without setting stored procedures or adding tables)
  • How to use MySQL DATEDIFF function to get the time interval between two dates
  • Mysql timeline data to obtain the first three data of the same day
  • MySQL example of getting today and yesterday's 0:00 timestamp
  • mysql gets yesterday's date, today's date, tomorrow's date, and the time of the previous hour and the next hour
  • mysql obtains statistical data within a specified time period
  • How to get time in mysql

<<:  Create a virtual machine and install the Redhat Linux operating system on VMware (graphic tutorial)

>>:  vue+rem custom carousel effect

Recommend

Usage of the target attribute of the html tag a

1: If you use the tag <a> to link to a page,...

Vue realizes the function of book shopping cart

This article example shares the specific code of ...

Navicat imports csv data into mysql

This article shares with you how to use Navicat t...

VUE + OPENLAYERS achieves real-time positioning function

Table of contents Preface 1. Define label style 2...

js regular expression lookahead and lookbehind and non-capturing grouping

Table of contents Combining lookahead and lookbeh...

Learn MySQL execution plan

Table of contents 1. Introduction to the Implemen...

How to install Docker using scripts under Linux Centos

What is the main function of Docker? At present, ...

The rel attribute of the HTML link tag

The <link> tag defines the relationship bet...

Perfect Solution for No rc.local File in Linux

Newer Linux distributions no longer have the rc.l...

Understanding and using callback functions in JavaScript

Table of contents Overview What are callbacks or ...

How to deploy ElasticSearch in Docker

1. What is ElasticSearch? Elasticsearch is also d...

JavaScript super detailed implementation of web page carousel

Table of contents Creating HTML Pages Implement t...

How to monitor Tomcat using LambdaProbe

Introduction: Lambda Probe (formerly known as Tom...

A simple way to implement all functions of shopping cart in Vue

The main functions are as follows: Add product in...

Linux kernel device driver advanced character device driver notes

/****************** * Advanced character device d...