When doing database statistics, you often need to collect data based on year, month, and day, and then use echarts to create visualizations. Database: MySQL Ideas The premise of statistics based on the time dimension is that the database needs to have retained time information. It is recommended to use the datetime type that comes with MySQL to record time.
The main function for processing time and date in MySQL is DATE_FORMAT(date,format). The available parameters are as follows
Note: When it comes to daily statistics, %j is required. If %d, %e, %w are used, the same values in different months/weeks will be counted together. When it comes to getting the current time, you can use now() or sysdate(). SELECT SYSDATE() FROM DUAL; SELECT NOW() FROM DUAL; You can use group by query according to actual needs. Conclusion <br /> The table structure to be counted is as follows: CREATE TABLE `apilog` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(64) DEFAULT NULL, `action` varchar(64) DEFAULT NULL, `params` text, `result` text, `timestamp` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) Count the number of actions of different categories within the time range # SELECT action on the day, COUNT(id) count FROM apilog WHERE DATE_FORMAT(`timestamp`,'%j') = DATE_FORMAT(now(),'%j') ORDER BY count desc; # This week SELECT action, COUNT(id) count FROM apilog WHERE DATE_FORMAT(`timestamp`,'%u') = DATE_FORMAT(now(),'%u') ORDER BY count desc; # Current month SELECT action, COUNT(id) count FROM apilog WHERE DATE_FORMAT(`timestamp`,'%m') = DATE_FORMAT(now(),'%m') ORDER BY count desc; # SELECT action, COUNT(id) count FROM apilog WHERE DATE_FORMAT(`timestamp`,'%Y') = DATE_FORMAT(now(),'%Y') ORDER BY count desc; Count the number of time dimensions of a certain classification action # By day SELECT action, DATE_FORMAT(`timestamp`,'%j'), COUNT(id) count FROM apilog WHERE action = 'xxx' GROUP BY DATE_FORMAT(`timestamp`,'%j') # By week SELECT action, DATE_FORMAT(`timestamp`,'%u'), COUNT(id) count FROM apilog WHERE action = 'xxx' GROUP BY DATE_FORMAT(`timestamp`,'%u') # By month SELECT action, DATE_FORMAT(`timestamp`,'%m'), COUNT(id) count FROM apilog WHERE action = 'xxx' GROUP BY DATE_FORMAT(`timestamp`,'%m') # By year SELECT action, DATE_FORMAT(`timestamp`,'%Y'), COUNT(id) count FROM apilog WHERE action = 'xxx' GROUP BY DATE_FORMAT(`timestamp`,'%Y') Statistics by action and time dimensions at the same time # By day SELECT action, DATE_FORMAT(`timestamp`,'%j'), COUNT(id) count FROM apilog GROUP BY action, DATE_FORMAT(`timestamp`,'%j') # By week SELECT action, DATE_FORMAT(`timestamp`,'%u'), COUNT(id) count FROM apilog GROUP BY action, DATE_FORMAT(`timestamp`,'%u') # By month SELECT action, DATE_FORMAT(`timestamp`,'%m'), COUNT(id) count FROM apilog GROUP BY action, DATE_FORMAT(`timestamp`,'%m') # By year SELECT action, DATE_FORMAT(`timestamp`,'%Y'), COUNT(id) count FROM apilog GROUP BY action, DATE_FORMAT(`timestamp`,'%Y') The above are the more commonly used time statistics. For more time dimensions, you can refer to the parameter table above for similar processing. You may also be interested in:
|
<<: Detailed Analysis of Event Bubbling Mechanism in JavaScript
>>: Detailed steps for installing and debugging MySQL database on CentOS7 [Example]
Why do I want to organize the content in this area...
Table of contents 1. Create HTML structure 2. Cre...
Online shopping mall database-user information da...
Function: data display, table application scenari...
First, let me give you an example (if you don’t w...
This article shares the specific code of jQuery t...
1. Install tools and libraries # PCRE is a Perl l...
1. Simple configuration of nginx's dynamic an...
This article shares the specific code of JavaScri...
Version update, the password field in the origina...
Table of contents 1. Pull the centos image 2. Bui...
Table of contents Why use setState Usage of setSt...
Find the problem I recently encountered a problem...
1. The role of brackets 1.1 Square brackets [ ] W...
<br />This web page production skills tutori...