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]
MySQL supports three types of comments: 1. From t...
The virtual machine is in use or cannot be connec...
Note: The nginx version must be 1.9 or above. Whe...
Table of contents 1. What is nginx? 2. What can n...
At the end of last year, I replaced the opensuse ...
Table of contents 1. Project Construction 2. Vue3...
Table of contents Method 1 Method 2 After install...
Install ssh tool 1. Open the terminal and type th...
This article mainly records a tomcat process, and...
Table of contents What is JSI What is different a...
The sudo command allows a trusted user to run a p...
exhibit design Password strength analysis The pas...
Find the running container id docker ps Find the ...
Function: Jump to the previous page or the next p...
1. IE8's getElementById only supports id, not ...